主要演示构造器,属性注入,BeanNameAware, BeanFactoryAware, ApplicationContextAware, EnvironmentAware, InitializingBean, DisposableBean, BeanPostProcessor, init-method, destroy-method 在整个Spring bean声明周期中的执行顺序
代码
Knight
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Knight implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, EnvironmentAware,
InitializingBean, DisposableBean {
private String name;
public Knight() {
System.out.println("constructor........");
}
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct");
}
@PreDestroy
public void preDestroy() {
System.out.println("preDestroy");
}
public void initMethod() {
System.out.println("initMethod");
}
public void destroyMethod() {
System.out.println("destroyMethod");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean destroy ");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean afterPropertiesSet ");
}
@Override
public void setEnvironment(Environment environment) {
System.out.println("EnvironmentAware Environment ");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware applicationContext ");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware beanFactory ");
}
@Override
public void setBeanName(String s) {
System.out.println("BeanNameAware setBeanName : " + s);
}
public String getName() {
return name;
}
@Value("${knight.name}")
public void setName(String name) {
System.out.println("setName........");
this.name = name;
}
}
KnightBeanPostProcessor
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class KnightBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Knight) {
System.out.println("BeanPostProcessor postProcessBeforeInitialization " + beanName);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Knight) {
System.out.println("BeanPostProcessor postProcessAfterInitialization " + beanName);
}
return bean;
}
}
BeanConfiguration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfiguration {
@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
public Knight knight() {
return new Knight();
}
}
启动和停止服务时结果
constructor........
setName........
BeanNameAware setBeanName : knight
BeanFactoryAware beanFactory
EnvironmentAware Environment
ApplicationContextAware applicationContext
BeanPostProcessor postProcessBeforeInitialization knight
postConstruct
InitializingBean afterPropertiesSet
initMethod
BeanPostProcessor postProcessAfterInitialization knight
Started DemoApplication in 2.377 seconds (JVM running for 4.985)
Disconnected from the target VM, address: '127.0.0.1:64272', transport: 'socket'
preDestroy
DisposableBean destroy
destroyMethod
|