一、这个接口有什么用?
严格上来说,方法类SpringContextUtil是一个bean,而且从步骤2)中我们不难发现,之所以方法类SpringContextUtil能够灵活自如地获取ApplicationContext就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类SpringContextUtil的方式告知spring有这样子一个类的存在。其实,方法很简单,就是将方法类SpringContextUtil作为一个普通的bean在spring的配置文件中进行注册:
以下是完整SpringContextUtil类实现:
当一个类实现了这个接口(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、方法类SpringContextUtil的处理
1)方法类SpringContextUtil实现ApplicationContextAware接口:
public class SpringContextUtil implements ApplicationContextAware
2)为方法类SpringContextUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext,就是通过读取这个成员变量的。具体如下所示:
private static ApplicationContext applicationContext;
实现ApplicationContextAware接口的默认方法:@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
3)在spring的配置文件中,注册方法类SpringContextUtil严格上来说,方法类SpringContextUtil是一个bean,而且从步骤2)中我们不难发现,之所以方法类SpringContextUtil能够灵活自如地获取ApplicationContext就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类SpringContextUtil的方式告知spring有这样子一个类的存在。其实,方法很简单,就是将方法类SpringContextUtil作为一个普通的bean在spring的配置文件中进行注册:
<bean id="SpringContextUtil" class="com.dhb.springmvc.util.SpringContextUtil"/>
4)使用静态的成员ApplicationContext类型的对象来调用其他bean。以下是完整SpringContextUtil类实现:
package com.dhb.springmvc.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Created by ${denghb} on 2016/8/21.
*/
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(String name, Class<T> requiredType) {
return applicationContext.getBean(name, requiredType);
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
}
注:加载Spring配置文件时,如果Spring配置文件中所定义的Bean类,如果该类实现了ApplicationContextAware接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware接口中的public void setApplicationContext(ApplicationContext context) throws BeansException方法,并且自动可获得ApplicationContext
对象。前提必须在Spring配置文件中指定改类。