java学习

java学习

 

spring bean 初始化方法和销毁方法的配置

三种初始化方式
public class Cat {
@PostConstruct//初始化
public void afterPropertiesSet() throws Exception {
System.out.println(" cat init");
}
@PreDestroy//销毁
public void destroy() throws Exception {
System.out.println("cat destroy");
}
}
public class User implements InitializingBean,DisposableBean{
public String toString() {
return "444";
}
@Override//初始化
public void afterPropertiesSet() throws Exception {
System.out.println(" user afterPropertiesSet");
}
@Override//销毁
public void destroy() throws Exception {
System.out.println("user destroy");
}
}
public class Room {
        //初始化
public void afterPropertiesSet() throws Exception {
System.out.println(" room afterPropertiesSet");
}
        //销毁
public void destroy() throws Exception {
System.out.println("room destroy");
}
}
@Configuration
public class Cfg1 {
@Bean
public Cat getCat() {
return new Cat();
}
@Bean
public User getUser() {
return new User();
}
@Bean(initMethod="afterPropertiesSet",destroyMethod="destroy")
public Room getRoom() {
return new Room();
}
}

posted on 2018-03-06 13:56 杨军威 阅读(233) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜