springbean生命周期
在springboot项目里验证springbean生命周期
点击查看代码
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component(value = "zhangsan")
public class Person implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, BeanFactoryPostProcessor,BeanPostProcessor,InitializingBean, DisposableBean {
private String name;
static void dosometing(){
System.out.println("Person 静态方法");
}
public Person() {
System.out.println("Person 类构造方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("set方法被调用");
}
//自定义的初始化函数
@PostConstruct
public void myInit() {
System.out.println("myInit被调用");
}
//自定义的销毁方法
@PreDestroy
public void myDestroy() {
System.out.println("myDestroy被调用");
}
// @Override
// public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// System.out.println("BeanPostProcessor接口 postProcessBeforeInitialization被调用");
// return bean;
// }
// @Override
// public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// System.out.println("BeanPostProcessor接口 postProcessAfterInitialization被调用");
// return bean;
// }
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("DisposableBean接口 destory被调用");
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("InitializingBean接口 afterPropertiesSet被调用");
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("ApplicationContextAware接口 setApplicationContext被调用");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanFactoryAware接口 setBeanFactory被调用,beanFactory:"+beanFactory);
}
public void setBeanName(String beanName) {
// TODO Auto-generated method stub
System.out.println("BeanNameAware接口 setBeanName被调用,beanName:" + beanName);
}
public String toString() {
return "name is :" + name;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("BeanFactoryPostProcessor接口 postProcessBeanFactory被调用");
}
}
本文来自博客园,作者:ControlO_o,转载请注明原文链接:https://www.cnblogs.com/ControlO-o/p/17682969.html