标签:source 使用 on() 实现 init cep beans tor 特殊情况
<bean id="student" class="org.ghl.entity.Student" scope="singleton" init-method="myInit" destroy-method="myDestory"> <property name="stuNo" value="4"></property> <property name="stuName" value="df"></property> <property name="stuAge" value="34"></property> </bean>
注解方式:
@Bean(value = "stu",initMethod = "myInit",destroyMethod = "myDestory")
总结:IOC容器在初始化时,会自动创建对象(即生成构造方法),再初始化init,当容器关闭时调用destory。
@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..."); } }
过程:将相应的组件加入@Conponent注解,Bean的名字是value值,初始化方法加 @PostConstruct,销毁方法加@PreDestroy。
@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;
标签:source 使用 on() 实现 init cep beans tor 特殊情况
原文地址:https://www.cnblogs.com/ghlz/p/13191808.html