spring多文件的几种配置方法

确实比较男 2010-08-26

spring多配置文件在web.xml中的几种配置方式:

往往在实际的开发当中会涉及到许多的spring配置文件。那么这些配置文件应该如何在web.xml中配置呢?

<1>:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/applicationContext-*.xml,classpath*:application-*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<2>:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/classes/applicationContext.xml

/WEB-INF/classes/applicationContext2.xml

</param-value>

</context-param>

<3>:

<beans>

<beanid="default"class="org.springframework.context.support.ClassPathXmlApplicationContext">

<constructor-arg>

<list>

<value>applicationContext.xml</value>

<value>applicationContext-business.xml</value>

<value>applicationContext-scheduler.xml</value>

</list>

</constructor-arg>

</bean>

</beans>

然后在web.xml文件中,使用自己定义的ContextLoaderServlet来启动,这个ContextLoaderServlet可以继承HttpServlet,

在它inti()时候取得BeanFactory,代码如下:

publicclassContextLoaderServletextendsHttpServlet{

publicContextLoaderServlet(){}

publicinit()throwsServletException{

//这个参数里的名字就是我们在BeanRefFactory.xml里定义的bean的名字。

DefaultBeanFactory.getFactory("default");

}

publicdestory(){}

}

然后定义一个DefaultBeanFactory类

publicclassDefaultBeanFactory(){

protercedstaticfinalBeanFactorygetFactory(Strings){

BeanFactoryLocatorbeanfactorylocator=SingletonBeanFactoryLocator.getInstance();

BeanFactoryReferencebeanfactoryreference=beanfactorylocator.useBeanFactory(s);

returnbeanfactoryreference.getFactory();

}

}

最后在web.xml中的配置

最后在web.xml文件中配置如下:

<servlet>

<servlet-name>context</servlet-name>

<servlet-class>com.tks.web.servlet.ContextLoaderServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>
在一个主spring配置文件中,import其它spring配置文件就行了

相关推荐

kyle00 / 0评论 2020-05-07
zhangdy0 / 0评论 2020-05-01
melonjj / 0评论 2020-02-18