Bean生命周期 Bean创建 -->初始化 -->销毁

1.自定义Bean初始化 和销毁的方法

init-method和destroy-method

创建Bike类

public class Bike {
public Bike(){
System.out.println("Bike Constructor...");
}
public void init(){
System.out.println("bike ...init...");
} public void destroy(){
System.out.println("bike ....destroy...");
}
}

配置类

@Configuration
public class MainConfig { @Bean(initMethod = "init",destroyMethod = "destroy")
public Bike bike(){
return new Bike();
}
}

测试

 @Test
public void test1(){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MainConfig.class);
context.close(); } 打印结果

Bike Constructor...
bike ...init...
十月 15, 2019 10:05:36 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5c0369c4: startup date [Tue Oct 15 10:05:35 CST 2019]; root of context hierarchy
bike ....destroy...

 

对于单实例的bean 可以正常调用初始化和销毁方法

对于多实例的bean( @Scope("prototype") )   容器只负责初始化(第一次获取该bean实例时才会创建并初始化) 但不会管理bean ,容器关闭时不会调用销毁方法

单例懒加载的bean (@lazy)第一次调用时初始化bean 容器会管理

2 .实现 InitializingBean 和 DisposableBean 接口

afterPropertiesSet() 当beanFactroy 创建好对象,并且把bean所有属性设置好之后,会调用这个方法。

destroy()  当一个单例bean销毁时 BeanFactory 会调用这个销毁方法 ,       在容器关闭时,应用上下文会销毁所有的单例bean。

@Component
public class Bike implements InitializingBean, DisposableBean {
public Bike(){
System.out.println("Bike Constructor...");
}
//当bean销毁时调用
public void destroy() throws Exception {
System.out.println("Bike destroy..");
}
//当bean 属性数赋值和初始化是调用
public void afterPropertiesSet() throws Exception {
System.out.println("Bike ....afterPropertiesSet...");
}
}

3.  使用JSR250 规则  定义的两个注解

@PostConstruct  在bean创建完成 ,且属性复制完成后进行初始化,属于JDK规范的注解

@PreDestroy  在bean将被移除之前进行通知,在容器晓辉之前进行清理工作

@Component
public class Bike {
public Bike(){
System.out.println("Bike Constructor...");
}
@PostConstruct
public void init(){
System.out.println("Bike...@PostConstruct...");
}
@PreDestroy
public void destroy(){
System.out.println("Bike...@@PreDestroy...");
}
}

4.  BeanPostProcessor    bean的后置处理器  ,在bean初始化之前调用进行拦截 ,在bean初始化前进行一些处理工作

使用 BeanPostProcessors 实现两个接口   postProcessBeforeInitialization 和 postProcessAfterInitialization

@Component
public class TestBeanPostProcessor implements BeanPostProcessor{ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//返回一个对象 (传过来的对象)
//在初始化方法调用之前进行后置处理
//init-mothed 调用之前
System.out.println("postProcessBeforeInitialization..."+beanName+"..."+bean);
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// init-monthod 调用之后
System.out.println("postProcessBeforeInitialization..."+beanName+"..."+bean);
return bean;
}
}
@Configuration
@ComponentScan("cn.qin")
public class MainConfig { @Bean
public Person person(){
System.out.println("给容器中添加 Person");
return new Person();
}
@Bean(initMethod = "init",destroyMethod = "destroy")
public Bike bike(){
return new Bike();
} }

bean初始化和销毁的几种方式的更多相关文章

  1. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  2. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  3. spring bean初始化及销毁你必须要掌握的回调方法

    spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...

  4. spring bean初始化和销毁

    spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性 ...

  5. Java 中初始化 List 集合的 6 种方式!

    List 是 Java 开发中经常会使用的集合,你们知道有哪些方式可以初始化一个 List 吗?这其中不缺乏一些坑,今天栈长我给大家一一普及一下. 1.常规方式 List<String> ...

  6. SpringBoot中资源初始化加载的几种方式(看这一片就够了)

    一.问题 在平时的业务模块开发过程中,难免会需要做一些全局的任务.缓存.线程等等的初始化工作,那么如何解决这个问题呢?方法有多种,但具体又要怎么选择呢? 二.资源初始化 1.既然要做资源的初始化,那么 ...

  7. Java 中初始化 List 集合的 7 种方式

    1.常规方式 List<String> languages = new ArrayList<>(); languages.add("Java"); lang ...

  8. SpringBoot中资源初始化加载的几种方式

    一.问题 在平时的业务模块开发过程中,难免会需要做一些全局的任务.缓存.线程等等的初始化工作,那么如何解决这个问题呢?方法有多种,但具体又要怎么选择呢? 二.资源初始化 1.既然要做资源的初始化,那么 ...

  9. Spring中bean的初始化和销毁几种实现方式

    Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...

随机推荐

  1. pycharm 如何自动添加头注释,比如时间,作者信息等

    查找路径:File->settings->Editor->File and Code Templates->Python Script #!/usr/bin/env pytho ...

  2. github高速下载的方法

    windows修改host文件: C:\Windows\System32\drivers\etc\hostslinux 修改host文件: /etc/hosts 在文件后面加上这两行 151.101. ...

  3. App 仿淘宝:控制详情和购买须知样式切换,控制商品详情和购买须知选项卡的位置(固定在顶部还是正常)

    CSS: <div id="details" ref="details" class="details" :class="t ...

  4. 附:常见的Jdbc Type 与 Java Type之间的关系

    附:常见的Jdbc Type 与 Java Type之间的关系 JDBC Type Java Type CHAR                  String VARCHAR String LONG ...

  5. luogu【模板】线性筛素数 (Miller-Rabin素数测试模板)

    这个感觉还是挺好理解的,就是复杂度证明看不懂~ Code: #include <cstdio> #include <algorithm> #include <cstrin ...

  6. hdu 5726 GCD GCD+线段树+区间预处理+map

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. ZOJ - 4045District Division dfs划分子树

    ZOJ - 4045District Division 题目大意:给你n个节点的树,然后让你划分这棵数使得,每一块都恰好k个节点并且两两间是连通的,也就是划分成n/k个连通集,如果可以输出YES,并输 ...

  8. AtCoder AGC005E Sugigma: The Showdown (博弈论)

    题目链接 https://atcoder.jp/contests/agc005/tasks/agc005_e 题解 完了真的啥都不会了-- 首先,显然如果某条A树的边对应B树上的距离大于等于\(3\) ...

  9. Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)

    题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...

  10. AcWing:165. 小猫爬山(dfs + 剪枝)

    翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). 翰翰和达达只好花钱让它们坐索道下山. 索道上的缆 ...