这篇文章是过于理论的东西,这里有一份能够直接使用的封装好的源码:Spring MVC3.2 通用获取bean封装源码 Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制。BeanFactory使管理不同类型的Java对象成为可能,应用上下文(com.springframework.context.ApplicationContext)建立在BeanFactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用。我们一般称BeanFactory为IoC容器,而称ApplicationContext为应用上下文。但有时为了行文方便,我们也将ApplicationContext称为Spring容器。 本文不涉及通过@Resource、@Autowired 自动注入,仅仅通过ApplicationContext获取Sping配置文件中的Bean。 要获取XML中配置的Bean,最关键的是获取org.springframework.context.ApplicationContext 第一种获取ApplicationContext的方法: import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml"); 或者 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 这种方式实例化applicationContext是非常耗时的,仅仅推荐使用在程序需要通过配置文件手工初始化Spring的情况。 第二种获取ApplicationContext的方法: Spring提供了WebApplicationContextUtils.getWebApplicationContext方法来获取ApplicationContext对象。 import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext; ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletContext servletContext); //ServletContext servletcontext = request.getSession().getServletContext(); 通过javax.servlet.ServletContext 获取到ApplicationContext实例对象,这意味着,必须使用到request、session等等。 注意:当使用WebApplicationContextUtils获取ApplicationContext实例时,需要在web.xml配置文件中添加org.springframework.web.context.ContextLoaderListener监听器,否则获取不到ApplicationContext对象,返回Null。 配置文件:web.xml <!--ContextLoaderListener自动注入 applicationContext,通过 WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext())获取 --> <!--Spring配置文件加载位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appContext.xml,/WEB-INF/spring/appInterceptor.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 第三种获取ApplicationContext的方法: 通过继承org.springframework.context.support.ApplicationObjectSupport使用getApplicationContext()获取ApplicationContext实例 第四种获取WebApplicationContext的方法: 通过继承org.springframework.web.context.support.WebApplicationObjectSupport使用getWebApplicationContext() 获取到org.springframework.web.context.WebApplicationContext WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 第五种获取ApplicationContext的方法: 通过实现org.springframework.context.ApplicationContextAware接口,实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。 Spring初始化时,会通过该方法将ApplicationContext对象注入。 第三、四、五种方法都需要将类配置在Spring配置文件中: <!--假定ApplicationContextTool为继承或者实现了第三、四、五种方法的具体实现类--> <bean class="com.twovv.utils.ApplicationContextTool"></bean> 否则将获取不到ApplicationContext,返回Null。 |
|