Spring--BeanFactory和ApplicationContext

1.BeanFactory和ApplicationContext的关系:

  1. ApplicationContext继承自BeanFactory,是BeanFactory的子接口,顾名思义用来管理bean,
    ApplicationContext.getBean(beanId)

  2. 它们是Spring IOC的关键,是IOC容器
  3. 通过指定的配置文件实例化bean和依赖注入(DI)
知道了这两者的关系,那下面就只说明ApplicationContext

2.ApplicationContext的使用

  • 手动初始化ApplicationContext,有3种方式:
  • ClassPathXmlApplicationContext
比较常用(web工程不推荐使用)
new ClassPathXmlApplicationContext("ConnectionConfig.xml");
  • FileSystemXmlApplicationContext

              不太常用

  • WebApplicationContextUtils

专门用于web工程,因为需要参数ServletContext。同时需要注册个ContextLoaderListener 来加载root context

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

        ServletContext servletContext = request.getSession().getServletContext();    
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        //ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

查看spring API可以发现

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
WebApplicationContextUtils.getWebApplicationContext(servletContext);
都是获取root context的,也就是说必须有ContextLoaderListener的声明。下边的代码可用来获取非root context的ApplicationContext,即某个servlet(spring)下的context(这种情况下不需要声明ContextLoaderListener,因为spring servlet中由DispatcherServlet初始化该servlet下的context):

        ServletContext servletContext = request.getSession().getServletContext();    
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext, FrameworkServlet.SERVLET_CONTEXT_PREFIX+"springMVC");
        //ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(httpServletRequest);

上边的"springMVC"是web.xml中的配置项:

<servlet-name>springMVC</servlet-name>

关于ContextLoaderListener和DispatcherServlet加载配置文件的区别请参考:

http://blog.csdn.net/ichsonx/article/details/7699093http://blog.csdn.net/prince2270/article/details/5889117

  • 利用web容器初始化ApplicationContext,适用于web工程,有两种方式ContextLoaderListener和ContextLoaderServlet:
  • <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:ConnectionConfig.xml</param-value>
        </context-param>
        <listener>   
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
       </listener>
    上边的param-name必须是contextConfigLocation,就是让ContextLoaderListener加载指定xml来初始化ApplicationContext。ConnectionConfig.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                            http://www.springframework.org/schema/context                            http://www.springframework.org/schema/context/spring-context-3.0.xsd                            http://www.springframework.org/schema/tx                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                            http://www.springframework.org/schema/aop                             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      <bean id="connectionFactory" class="dao.ConnectionFactory">
          <property name="jndi" value="oracleJNDIDataSource"/>
          <property name="wLInitialContextFactory" value="weblogic.jndi.WLInitialContextFactory"/>
          <property name="contextProviderURL" value="t3://127.0.0.1:7101"/>
      </bean> 
      <bean id="beanManager" class="beanmanager.BeanManager"></bean> 
</beans>

BeanManager 类实现了ApplicationContextAware接口,所以初始化ApplicationContext的时候,通过set方法把ApplicationContext传入BeanManager,如下:

public class BeanManager implements ApplicationContextAware{
    private static ApplicationContext context;
    public BeanManager() {
        super();
    }
    public static Object getBean(String beanId)throws Exception{
        Object o = null;
        //ClassPathXmlApplicationContext context = getContext();
        if(context!=null){
            o = context.getBean(beanId);
            System.out.println(context.getDisplayName()+"dfsdf");
        }
        return o;
    }

    public void setContext(ClassPathXmlApplicationContext context) {
        this.context = context;
    }

//    public static ClassPathXmlApplicationContext getContext() {
//        if(context == null)
//            context = new ClassPathXmlApplicationContext("ConnectionConfig.xml");
//        return context;
//    }

    public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) {
        context = applicationContext;
    }
}

  • <context-param>

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

      <param-value>classpath:spring-config/applicationContext.xml</param-value>

      </context-param>

      <!-- 指定以Servlet方式启动Spring容器 -->

      <servlet>

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

      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

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

      </servlet>


    其余同第一种方式


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值