public interface ApplicationContextAware extends Aware
ApplicationContext
that it runs in.
Implementing this interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.
大概意思是:任意想得到正在运行的 ApplicationContext 的对象需要实现该接口。
实现这个接口是有意义的,比如当一个对象请求访问有协作关系的bean集合时,注意只能通过配置实现了这个接口的bean来查找目标。
补充说明一下:如果实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法。
举例:
public class Test implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext context) throws BeansException {
Bean1 b = (Bean1) context.getBean("bean1");
System.out.println("打印 : " + b.getName());
}
}