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>
<bean id="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,代码如下:
public class ContextLoaderServlet extends HttpServlet {
public ContextLoaderServlet() {}
public init() throws ServletException{
//这个参数里的名字就是我们在BeanRefFactory.xml里定义的bean的名字。
DefaultBeanFactory.getFactory("default");
}
public destory() {}
}
然后定义一个DefaultBeanFactory类
public class DefaultBeanFactory () {
proterced static final BeanFactory getFactory (String s) {
BeanFactoryLocator beanfactorylocator = SingletonBeanFactoryLocator.getInstance();
BeanFactoryReference beanfactoryreference = beanfactorylocator.useBeanFactory(s);
return beanfactoryreference.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>