BeanFactoryPostProcessor接口中只有一个方法postProcessBeanFactory,该方法在spring容器初始化后执行,并且只执行一次。它会在BeanPostProcessor中的方法执行之前先执行。示例如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>spring</artifactId> <version>1.0.0</version> <name>spring</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> </dependencies> </project>
User.java
package com.edu.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; public class User { private ApplicationContext applicationContext; public void init(){ System.out.println("User init()"); } public void show(){ System.out.println("User show(): "+applicationContext); } @Autowired public void setApplicationContext(ApplicationContext applicationContext) { System.out.println("------set()-----"); this.applicationContext = applicationContext; } }
MyConfig.java
package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean(initMethod="init") public User createUser(){ return new User(); } }
EchoBeanPostProcssor.java
package com.edu.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; @Component public class EchoBeanPostProcssor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("-------------postProcessBeforeInitialization-------------"+bean.getClass()); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("-------------postProcessAfterInitialization-------------"+bean.getClass()); return bean; } }
MyBeanFactoryPostProcessor.java
package com.edu.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component; @Component public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { // TODO Auto-generated method stub System.out.println("--------------------"+beanFactory.getBeanDefinitionCount()); } }
运行结果如下: