关于org.springframework.context.ApplicationContextAware类的使用理解

 一、这个接口有什么用?

当一个类实现了这个接口(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>





//判断类资源的工具类

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * 工具类
 * 
 */
@SuppressWarnings("static-access")
public class CacheContextUtil implements ApplicationContextAware {
    private static ApplicationContext commonApplicationContext;


    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
this.commonApplicationContext = context;
    }


    /**
     * 根据提供的bean名称得到相应的服务类
     * @param beanId bean的id
     * @return 返回bean的实例对象
     */
    public static Object getBean(String beanId) {
return commonApplicationContext.getBean(beanId);
    }


    /**
     * 根据提供的bean名称得到对应于指定类型的服务类
     * @param beanId bean的id
     * @param clazz bean的类类型
     * @return 返回的bean类型,若类型不匹配,将抛出异常
     */
    public static <T> T getBean(String beanId, Class<T> clazz) {
return commonApplicationContext.getBean(beanId, clazz);
    }


    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     * @param beanId bean的id
     * @return boolean
     */
    public static boolean containsBean(String beanId) {
return commonApplicationContext.containsBean(beanId);
    }


    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     * @param beanId bean的id
     * @return 是否是单例模式
     */
    public static boolean isSingleton(String beanId) {
return commonApplicationContext.isSingleton(beanId);
    }


    /**
     * @param type bean的类型
     * @return Class 注册对象的类型
     */
    @SuppressWarnings("rawtypes")
    public static Class getType(String type) {
return commonApplicationContext.getType(type);
    }


    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     * @param beanId XML文件中配置的bean id
     * @return bean的别名
     */
    public static String[] getAliases(String beanId) {
return commonApplicationContext.getAliases(beanId);
    }


    @SuppressWarnings("unchecked")
    public static <T> T getBeanByName(String beanName) {
try {
    return (T) commonApplicationContext.getBean(beanName);
} catch (Exception e) {
    return null;
}
    }


    public static <T> T getBeanByType(Class<T> type) {
try {
    return commonApplicationContext.getBean(type);
} catch (Exception e) {
    return null;
}
    }


}

猜你喜欢

转载自blog.csdn.net/Angle_wing_wh/article/details/79004423