Spring提供了许多***Aware接口来获取容器中的各种组件
***Aware 都定义了一个set***(XXX xx)方法需要子类bean去实现 。
在Spring初始化bean时 ,会对该bean的类型进行检查,判断是否实现了**Aware接口,如果实现了,就调用它对应的**set方法,并将对应的对象设置进去 。
以ApplicationContextAware为例,它用于获取ApplicationContext对象, 它的定义如下:
public interface ApplicationContextAware {
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see org.springframework.beans.factory.BeanInitializationException
*/
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
实例:在我创建的Service中获取容器中的ResourceLoader、ApplicationEventPublisher、beanName、BeanFactory、MessageSource、ApplicationContext
package com.spring.service.impl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import com.spring.service.PersonService;
public class PersonServiceImpl implements PersonService, ApplicationContextAware, MessageSourceAware, BeanFactoryAware, BeanNameAware, ApplicationEventPublisherAware, ResourceLoaderAware {
private String name;
private ResourceLoader resourceLoader;
private ApplicationEventPublisher applicationEventPublisher;
private String beanName;
private BeanFactory beanFactory;
private MessageSource messageSource;
private ApplicationContext applicationContext;
public void sayHello() {
System.out.println("Hello World!");
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public void setBeanName(String name) {
this.beanName = name;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}