spring获取webapplicationcontext,applicationcontext几种方法

1.加载XML文件获取

其实(不可取)

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

这种方式获取Sping上下文环境,最主要是在测试环境中使用,比如写一个测试类,系统不启动的情况下手动初始化Spring上下文再获取对象!

2.通过Spring提供的工具类获取ApplicationContext对象

import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
ServletContext sc 可以通过 HttpServletRequest request变量请求中获取
根据ServletContext获取webApplicationConext
ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

3.添加监听类

web.xml:

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

    <listener>
        <listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
    </listener>
其中SpringInit实现接口ServletContextListener :

package com.ibatis.jpetstore.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class SpringInit implements ServletContextListener {


    private static WebApplicationContext springContext;

    public SpringInit() {
        super();
    }

    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }


    public void contextDestroyed(ServletContextEvent event) {
    }

    public static ApplicationContext getApplicationContext() {
        return springContext;
    }


}

在其中的一个bean的构造里SpringInit获取applicationcontext,代码:

  public OrderBean() {
    this(
            (AccountService) SpringInit.getApplicationContext().getBean("accountService"),
            (OrderService) SpringInit.getApplicationContext().getBean("orderService") );
  }

4.继承自抽象类ApplicationObjectSupport 

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。 
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

5.继承自抽象类WebApplicationObjectSupport 

说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

6.实现接口ApplicationContextAware () (推荐)

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。 
Spring初始化时,会通过该方法将ApplicationContext对象注入。

@Component
public class SpringContextUtil implements ApplicationContextAware {
         private static ApplicationContext applicationContext; // Spring应用上下文环境
         /*
          * 实现了ApplicationContextAware 接口,必须实现该方法;
          *通过传递applicationContext参数初始化成员变量applicationContext
          */
         public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
               SpringContextUtil.applicationContext = applicationContext;
         }
 
         public static ApplicationContext getApplicationContext() {
                return applicationContext;
         }

          @SuppressWarnings("unchecked")
          public static <T> T getBean(String name) throws BeansException {
                     return (T) applicationContext.getBean(name);
           }
}

 

注意:这个地方使用了Spring的注解@Component,如果不是使用annotation的方式,而是使用xml的方式管理Bean,记得写入配置文件

<bean id="springContextUtil" class="com.ecdatainfo.util.SpringContextUtil" singleton="true" />

有关webapplicationcontext,applicationcontext关系可以参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值