date[2019-06-02]
User.java
package edu.cninfo.processor;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class User {
@PostConstruct
public void init(){
System.out.println("==========init user========");
}
@PreDestroy
public void destory(){
System.out.println("========destory user=======");
}
}
MyConfig.java
package edu.cninfo.processor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public User createUser(){
return new User();
}
}
BeanPostProcessor.java
package edu.cninfo.processor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyBeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("=========postProcessBeforeInitialization=========" + bean.getClass());
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("=========postProcessAfterInitialization=========" + bean.getClass());
return bean;
}
}
BeanFactoryPostProcessor.java
package edu.cninfo.processor;
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 {
System.out.println("count["+beanFactory.getBeanDefinitionCount()+"]");
}
}
AwareProcessorApp.java
package edu.cninfo.processor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AwareProcessorApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext("edu.cninfo.processor");
acc.close();
}
}
运行结果
C:\soft\jdk1.8.0_202\bin\java.exe -javaagent:C:\soft\IDEA2018.3\lib\idea_rt.jar=64220:C:\soft\IDEA2018.3\bin -Dfile.encoding=UTF-8 -classpath C:\soft\jdk1.8.0_202\jre\lib\charsets.jar;C:\soft\jdk1.8.0_202\jre\lib\deploy.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\soft\jdk1.8.0_202\jre\lib\javaws.jar;C:\soft\jdk1.8.0_202\jre\lib\jce.jar;C:\soft\jdk1.8.0_202\jre\lib\jfr.jar;C:\soft\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\soft\jdk1.8.0_202\jre\lib\jsse.jar;C:\soft\jdk1.8.0_202\jre\lib\management-agent.jar;C:\soft\jdk1.8.0_202\jre\lib\plugin.jar;C:\soft\jdk1.8.0_202\jre\lib\resources.jar;C:\soft\jdk1.8.0_202\jre\lib\rt.jar;D:\code\IdeaProjects\spring\learn-spring4-03\target\classes;C:\Users\Chen\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot-starter\2.1.0\jasypt-spring-boot-starter-2.1.0.jar;C:\Users\Chen\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot\2.1.0\jasypt-spring-boot-2.1.0.jar;C:\Users\Chen\.m2\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-context\5.0.12.RELEASE\spring-context-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-aop\5.0.12.RELEASE\spring-aop-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-expression\5.0.12.RELEASE\spring-expression-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-web\5.0.12.RELEASE\spring-web-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-beans\5.0.12.RELEASE\spring-beans-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-core\5.0.12.RELEASE\spring-core-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-jcl\5.0.12.RELEASE\spring-jcl-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-core\2.6.2\log4j-core-2.6.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-api\2.6.2\log4j-api-2.6.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-jcl\2.6.2\log4j-jcl-2.6.2.jar;C:\Users\Chen\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-slf4j-impl\2.6.2\log4j-slf4j-impl-2.6.2.jar;C:\Users\Chen\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\Chen\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;C:\Users\Chen\.m2\repository\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar edu.cninfo.processor.AwareProcessorApp
21:30:40.651 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:30:40.651 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:30:40.697 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
21:30:40.697 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:30:41.088 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBeanFactoryPostProcessor'
21:30:41.088 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'myBeanFactoryPostProcessor'
21:30:41.088 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'myBeanFactoryPostProcessor' to allow for resolving potential circular references
21:30:41.202 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'myBeanFactoryPostProcessor'
count[10]
21:30:41.206 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:30:41.206 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:30:41.208 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
21:30:41.217 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:30:41.232 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:30:41.248 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
21:30:41.263 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:30:41.263 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBeanProcessor'
21:30:41.263 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'myBeanProcessor'
21:30:41.279 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'myBeanProcessor' to allow for resolving potential circular references
21:30:41.279 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'myBeanProcessor'
21:30:41.279 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@43ee72e6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,myBeanFactoryPostProcessor,myBeanProcessor,myConfig,createUser]; root of factory hierarchy
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
21:30:41.295 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
=========postProcessBeforeInitialization=========class org.springframework.context.event.EventListenerMethodProcessor
=========postProcessAfterInitialization=========class org.springframework.context.event.EventListenerMethodProcessor
21:30:41.310 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
21:30:41.326 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
21:30:41.326 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
21:30:41.326 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
=========postProcessBeforeInitialization=========class org.springframework.context.event.DefaultEventListenerFactory
=========postProcessAfterInitialization=========class org.springframework.context.event.DefaultEventListenerFactory
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'myBeanFactoryPostProcessor'
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'myBeanProcessor'
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myConfig'
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'myConfig'
21:30:41.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'myConfig' to allow for resolving potential circular references
=========postProcessBeforeInitialization=========class edu.cninfo.processor.MyConfig$$EnhancerBySpringCGLIB$$876e299d
=========postProcessAfterInitialization=========class edu.cninfo.processor.MyConfig$$EnhancerBySpringCGLIB$$876e299d
21:30:41.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'myConfig'
21:30:41.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'createUser'
21:30:41.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'createUser'
21:30:41.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'myConfig'
21:30:41.452 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'createUser' to allow for resolving potential circular references
=========postProcessBeforeInitialization=========class edu.cninfo.processor.User
==========init user========
=========postProcessAfterInitialization=========class edu.cninfo.processor.User
21:30:41.461 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'createUser'
21:30:41.461 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
21:30:41.540 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:30:41.564 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:30:41.566 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@43ee72e6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,myBeanFactoryPostProcessor,myBeanProcessor,myConfig,createUser]; root of factory hierarchy
========destory user=======
Maven配置如下
<?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>manning</groupId>
<artifactId>learn-spring4-01</artifactId>
<version>1.0</version>
<description>spring-framework-reference demo1</description>
<properties>
<java.version>1.8</java.version>
<maven.compile.target>1.8</maven.compile.target>
<maven.compile.source>1.8</maven.compile.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.12.RELEASE</spring.version>
<!--<spring.version>4.3.23.RELEASE</spring.version>-->
<log4j.version>1.2.17</log4j.version>
<log4j-core.version>2.6.2</log4j-core.version>
<log4j.version>2.6.2</log4j.version>
<lombok.version>1.18.8</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--@Slf4j-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>io.spring.repo.maven.release</id>
<url>http://repo.spring.io/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>