Spring8——Bean的生命周期、自动装配
2021-05-05 22:27
标签:source 使用 on() 实现 init cep beans tor 特殊情况 注解方式: 总结:IOC容器在初始化时,会自动创建对象(即生成构造方法),再初始化init,当容器关闭时调用destory。 过程:将相应的组件加入@Conponent注解,Bean的名字是value值,初始化方法加 @PostConstruct,销毁方法加@PreDestroy。 Spring8——Bean的生命周期、自动装配 标签:source 使用 on() 实现 init cep beans tor 特殊情况 原文地址:https://www.cnblogs.com/ghlz/p/13191808.html@Bean(value = "stu",initMethod = "myInit",destroyMethod = "myDestory")
@Component(value = "myConvert")
public class MyIntToStringConverter {
//初始化
@PostConstruct
public void init(){
System.out.println("转换器...init...");
}
public void myConverter(){
System.out.println("转换器...");
}
//销毁
@PreDestroy
public void destory(){
System.out.println("转换器...destory...");
}
}
@Component
public class MyFunction implements InitializingBean,DisposableBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Function...init");
}
public void Function(){
}
@Override
public void destroy() throws Exception {
System.out.println("Function...destory");
}
}
@Component
public class MyFourth implements BeanPostProcessor{
//拦截
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化:"+beanName+":"+bean);
System.out.println("MyFourth...init");
if (bean instanceof Student){
Student stu=(Student)bean;
stu.setStuName("sadafdf");
stu.setStuNo(234);
return stu;
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("销毁:"+beanName+":"+bean);
System.out.println("MyFourth...destory");
return bean;
}
}
@Autowired //从IOC容器中寻找一个类型为StudentDao的Bean(根据类型)
private StudentDao studentDao;
文章标题:Spring8——Bean的生命周期、自动装配
文章链接:http://soscw.com/index.php/essay/82923.html