Spring—ApplicationContextAware

来源:互联网 发布:cad软件是什么软件 编辑:程序博客网 时间:2024/06/04 18:36

项目用到了ApplicationContextAware,通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。

我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。

使用方法如下:

1.实现ApplicationContextAware接口:

[java] view plain copy
  1. package com.bis.majian.practice.module.spring.util;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.ApplicationContextAware;  
  6.   
  7. public class SpringContextHelper implements ApplicationContextAware {  
  8.     private static ApplicationContext context = null;  
  9.   
  10.     @Override  
  11.     public void setApplicationContext(ApplicationContext applicationContext)  
  12.             throws BeansException {  
  13.         context = applicationContext;  
  14.     }  
  15.       
  16.     public static Object getBean(String name){  
  17.         return context.getBean(name);  
  18.     }  
  19.       
  20. }  


2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.     http://www.springframework.org/schema/tx   
  8.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  9.     http://www.springframework.org/schema/context   
  10.     http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">  
  11.       
  12.     <bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean>  
  13.       
  14.     <context:component-scan base-package="com.bis.majian.practice.module.*" />  
  15. </beans>  


3.在web项目中的web.xml中配置加载Spring容器的Listener:

[html] view plain copy
  1. <!-- 初始化Spring容器,让Spring容器随Web应用的启动而自动启动 -->  
  2.     <listener>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  


4.在项目中即可通过这个SpringContextHelper调用getBean()方法得到Spring容器中的对象了。


参考:http://blog.csdn.net/majian_1987/article/details/11170841


=========================================================================================================================


 一、这个接口有什么用?

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。

二、怎么用?

举个例子吧:

例如我有一个方法类AppUtil,这个方法类中需要使用到的ApplicationContext中的某个bean(companyService)。

1、因为spring要建立属于自己的容器,就必须要加载自己的配置文件。

     这个时候,需要注册ContextLoaderListener或者这个类的子类。

在web.xml加上以下的信息:

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

当然,这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中

添加如下信息。可以参考我的示例,指定配置文件,如下:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/app-context.xml</param-value>
 </context-param>

 

注意:<param-name>contextConfigLocation</param-name>是不能改变的。

 

2、方法类AppUtil的处理

方法类AppUtil实现ApplicationContextAware接口:

public class AppUtil
  implements ApplicationContextAware

为方法类AppUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext,就是通过读取这个

成员变量的。具体如下所示:

private static ApplicationContext appContext;

 

实现ApplicationContextAware接口的默认方法:

 public void setApplicationContext(ApplicationContext paramApplicationContext)
    throws BeansException
  {
    appContext = paramApplicationContext;
  }

 

3、在spring的配置文件中,注册方法类AppUtil

严格上来说,方法类AppUtil是一个bean,而且从步骤2中我们不难发现,之所以方法类AppUtil能够灵活自如地获取ApplicationContext

就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要

通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。

其实,方法很简单,就是将方法类AppUtil作为一个普通的bean在spring的配置文件中进行注册:

<bean id="appUtil" class="com.htsoft.core.util.AppUtil"/>

4、使用静态的成员ApplicationContext类型的对象,appContext,来调用其他bean。在方法类AppUtil中增加如下方法:

public static Object getBean(String paramString)
  {
    return appContext.getBean(paramString);
  }

那么,在

方法类AppUtil中就能够灵活地调用其他任何一个bean了,例如:

CompanyService localCompanyService = (CompanyService)getBean("companyService");

注:配置文件中关于companyService的内容:

<bean id="companyService" class="com.kaiwii.service.system.impl.CompanyServiceImpl">
        <constructor-arg index="0" ref="companyDao"/>      
</bean>

 

参考:http://blog.csdn.net/kaiwii/article/details/6872642