spring多文件的几种配置方法

 

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>

在一个主spring配置文件中,import其它spring配置文件就行了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值