实例化spring容器applicationContext的几种方法

一、定义web.xml,由web容器自动加载配置文件初始化ApplicationContext实例,用WebApplicationContextUtils.getWebApplicationContext()得到ApplicationContext的引用。

方法1(web.xml)
<web-app>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
   </context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
方法2 (web.xml)
<web-app>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
   </context-param>
   <servlet>
      <servlet-name>SpringContextServlet</servlet-name>
      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
</web-app>
方法3 在(struts-config.xml)中加载
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property property="contextConfigLocation"
            value="/WEB-INF/applicationContext.xml,
                   /WEB-INF/action-servlet.xml"/>
</plug-in>

注:对于方法1,2如果xml配置文件比较多,可进行拆分,然后在applicationContext.xml配置文件加载被拆分的配置文件,通过<import resource=""/>加载被拆分的配置文件,如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="
http://www.springframework.org/schema/beans
"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
">
 
 <!-- 加载被拆分的Spring配置文件 -->
    <import resource="applicationContext-dao.xml"/>  
    <import resource="applicationContext-service.xml"/>

 

</beans>

 

通过web.xml配置,web容器会自动加载context-param中的配置文件初始化ApplicationContext实例,然后就可以在web应用中通过WebApplicationContextUtils.getWebApplicationContext方法获取ApplicationContext的引用

如:

    ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
    LoginAction action=(LoginAction)ctx.getBean("action");

 

编写SpringUtil.java,在构造方法中如下:

二、利用ClassPathXmlApplicationContext加载配置文件实例化applicationContext

     如下:

         ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-    INF/applicationContext.xml"); 

     或者

        String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};   

       ApplicationContext ac = new ClassPathXmlApplicationContext(locations);
     A a = (A)ac.getBean("a");

三、利用FileSystemXmlApplicationContext加载配置文件实例化applicationContext

    ApplicationContext ctx = new FileSystemXmlApplicationContext("/WEB-INF/applicationContext.xml");

    或者

    String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};            

    ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );

    A a =(A)ctx.getBean("a");

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值