需求:我们的需求是,在spring初始化完毕时,使我们自定义一个类Bird类可以得到spring容器内容。
实现步骤:
1.首先我们来看一下ApplicationContextAwareProcessor这个类,它是BeanPostProcessor(后置处理器)的一个实现类。所以ApplicationContextAwareProcessor里面也必定有后置处理器接口的两个前置和后置方法。
我们来看一下ApplicationContextAwareProcessor的前置方法:由前面讲解的后置处理器(链接:https://www.cnblogs.com/WNof11020520/p/10598745.html)可知,所有bean对象初始化前都会执行BeanPostProcessor接口实现类的前置方法postProcessBeforeInitialization,所以所有bean对象初始化前都会执行ApplicationContextAwareProcessor的postProcessBeforeInitialization方法。
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if(System.getSecurityManager() != null && (bean instanceof EnvironmentAware ||
bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware ||
bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware ||
bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
} if(acc != null) {
AccessController.doPrivileged(() -> {
this.invokeAwareInterfaces(bean);
return null;
}, acc);
} else {
this.invokeAwareInterfaces(bean);
} return bean;
} private void invokeAwareInterfaces(Object bean) {
if(bean instanceof Aware) {
if(bean instanceof EnvironmentAware) {
((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment());
} if(bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(this.embeddedValueResolver);
} if(bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext);
} if(bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext);
} if(bean instanceof MessageSourceAware) {
((MessageSourceAware)bean).setMessageSource(this.applicationContext);
} if(bean instanceof ApplicationContextAware) {
((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
}
} }
2.看以上代码第9、15、19、48行可知,假若bean对象的类是ApplicationContextAware类型的话,就会调用invokeAwareInterfaces方法,然后执行第48行,将spring容器赋值给bean对象的applicationContext属性。
所以重要的实现思路来了:
a.Bird类实现ApplicationContextAware接口,并添加一个ApplicationContext类型的属性context,并实现ApplicationContextAware接口的方法:
@Component
public class Bird implements ApplicationContextAware {
//得到管理它的Spring容器
private ApplicationContext context; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
} @Override
public String toString() {
return "Bird [context=" + context + "]";
}
}
b.写一个测试类,测试结果:
public class TestMain { public static void main(String[] args) {
// 1.获得Spring容器对象
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
System.out.println("Spring容器 " + context);
Bird bird = (Bird) context.getBean("bird");
System.out.println(bird);
}
}
c.查看测试输出的结果:输出结果显示,TestMain 类的main方法中new出来的context和Bird组件的bean对象的context属性地址相同,所以Bird的bean对象得到了spring容器内容。
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52099,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\kaikeba_java\2018-11-28-spring-beanpostprocessor\02-项目\11_Spring_BeanPostProcessor的实现类介绍\target\test-classes;D:\kaikeba_java\2018-11-28-spring-beanpostprocessor\02-项目\11_Spring_BeanPostProcessor的实现类介绍\target\classes;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-context\5.0.5.RELEASE\spring-context-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-aop\5.0.5.RELEASE\spring-aop-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-beans\5.0.5.RELEASE\spring-beans-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-core\5.0.5.RELEASE\spring-core-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-jcl\5.0.5.RELEASE\spring-jcl-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\apache-maven-3.5.3-bin\repo\org\springframework\spring-expression\5.0.5.RELEASE\spring-expression-5.0.5.RELEASE.jar;D:\caowenlong\javaDevelopTool\IntelliJ IDEA 2016.2\lib\idea_rt.jar" com.kkb.test.TestMain
Connected to the target VM, address: '127.0.0.1:52099', transport: 'socket'
三月 26, 2019 11:26:26 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ed94a8b: startup date [Tue Mar 26 11:26:26 CST 2019]; root of context hierarchy
Spring容器 org.springframework.context.annotation.AnnotationConfigApplicationContext@2ed94a8b: startup date [Tue Mar 26 11:26:26 CST 2019]; root of context hierarchy
Bird [context=org.springframework.context.annotation.AnnotationConfigApplicationContext@2ed94a8b: startup date [Tue Mar 26 11:26:26 CST 2019]; root of context hierarchy]
Disconnected from the target VM, address: '127.0.0.1:52099', transport: 'socket' Process finished with exit code 0