1.BeanFactory和ApplicationContext的关系:
知道了这两者的关系,那下面就只说明ApplicationContext
- ApplicationContext继承自BeanFactory,是BeanFactory的子接口,顾名思义用来管理bean,
ApplicationContext.getBean(beanId)
- 它们是Spring IOC的关键,是IOC容器
- 通过指定的配置文件实例化bean和依赖注入(DI)
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);
都是获取root context的,也就是说必须有ContextLoaderListener的声明。下边的代码可用来获取非root context的ApplicationContext,即某个servlet(spring)下的context(这种情况下不需要声明ContextLoaderListener,因为spring servlet中由DispatcherServlet初始化该servlet下的context):WebApplicationContextUtils.getWebApplicationContext(servletContext);
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/7699093,http://blog.csdn.net/prince2270/article/details/5889117
- 利用web容器初始化ApplicationContext,适用于web工程,有两种方式ContextLoaderListener和ContextLoaderServlet:
上边的param-name必须是contextConfigLocation,就是让ContextLoaderListener加载指定xml来初始化ApplicationContext。ConnectionConfig.xml配置如下:<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>
<?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>
其余同第一种方式