bean的生命周期:创建---初始化---销毁。

Spring中声明的Bean的初始化和销毁方法有3种方式:

  1. @Bean的注解的initMethod、DestroyMethod属性

  2. bean实现InitializingBean、DisposableBean接口

  3. @PostConstruct、@PreDestroy注解

  4. BeanPostProcessor(这种仅仅增强了Bean的初始化方法

@Bean的注解的initMethod、DestroyMethod属性

--cat类

public class Cat {

    public Cat() {
System.out.println("cat...constructor...");
} public void init() {
System.out.println("cat...init...");
} public void destroy() {
System.out.println("cat...destory...");
}
}

--配置类

@Configuration
public class SpringConfig { @Bean(name = "cat", initMethod = "init", destroyMethod = "destroy")
public Cat getCat() {
return new Cat();
}
}

--测试代码

@Test
public void testLifeCycle() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("IOC容器创建了...");
context.close();
}

--测试结果

bean实现InitializingBean、DisposableBean接口

实现InitialzingBean接口的afterPropertiesSet方法,即为bean的初始化方法。

实现DisposableBean接口的destroy方法,即为bean的销毁方法。

--dog类

public class Dog implements InitializingBean, DisposableBean {

    public Dog() {
System.out.println("dog...constructor...");
} // Dog类的初始化方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("dog...afterPropertiesSet...");
} // Dog类的销毁方法
@Override
public void destroy() throws Exception {
System.out.println("dog...destroy...");
}
}

--配置类

@Configuration
public class SpringConfig { @Bean(name = "dog")
public Dog getDog() {
return new Dog();
}
}

--测试代码

@Test
public void testLifeCycle() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("IOC容器创建了...");
context.close();
}

--测试结果

@PostConstruct、@PreDestroy注解

这两个注解只能标注在方法上,并且@PostConstruct注解标注的方法为bean的初始化方法,@PreDestroy标注的方法为bean的销毁方法。

--tiger类

public class Tiger {

    public Tiger() {
System.out.println("tiger...constructor");
} @PostConstruct
public void init() {
System.out.println("tiger...init...");
} @PreDestroy
public void destroy() {
System.out.println("tiger...destroy...");
}
}

--配置类

@Configuration
public class SpringConfig { @Bean(name = "tiger")
public Tiger getTiger() {
return new Tiger();
}
}

--测试代码

@Test
public void testLifeCycle() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("IOC容器创建了...");
context.close();
}

--测试结果

BeanPostProcessor

BeanPostProcessor是一个接口,这个接口中的postProcessBeforeInitializationpostProcessAfterInitialization方法分别在Bean的初始化方法执行的前后执行,本质上不算是声明Bean的初始化和销毁方法的一种,而是仅仅对bean的初始化方法的一个功能增强,并且在Spring中使用非常广泛,例如DI注解Autowired,当IOC容器调用无参构造创建对象之后,就会进行依赖注入,而且依赖注入在Bean的初始化方法执行之前,所以可以适用BeanPostProcessor来完成这一功能,Spring底层也是这么完成的。

--MyBeanPostProcessor

public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
* 在初始化之前执行
* @param bean 初始化的bean
* @param beanName bean的名字
* @return 初始化的bean或者包装过的bean
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " ==> " + "postProcessBeforeInitialization...");
return bean;
} /**
* 在初始化之后执行
* @param bean 初始化的bean
* @param beanName bean的名字
* @return 初始化的bean或者包装过的bean
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " ==> " + "postProcessAfterInitialization...");
return bean;
}
}

--配置类

@Configuration
public class SpringConfig { @Bean(name = "tiger")
public Tiger getTiger() {
return new Tiger();
} @Bean(name = "myBeanPostProcessor")
public MyBeanPostProcessor getMyBeanPostProcessor() {
return new MyBeanPostProcessor();
}
}

--测试代码

@Test
public void testLifeCycle() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("IOC容器创建了...");
context.close();
}

--测试结果

【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式的更多相关文章

  1. Spring Framework核心概念之Bean生命周期管理

    目录 Spring Bean的生命周期 相关接口的分类 测试SpringBean生命周期的Demo程序 小结 Spring Bean的生命周期 Spring容器既Application或者WebApp ...

  2. Spring bean 实现初始化、销毁方法的方式及顺序

    Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...

  3. Spring之使用注解实例化Bean并注入属性

    1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...

  4. Spring中用@DependsOn注解控制Bean的创建顺序

    1. 概述 Spirng容器自己会管理bean的生命周期和bean实例化的顺序,但是我们仍然可以根据我们自己的需求进行定制.我可以可以选择使用SmartLifeCycle接口,也可以用@Depends ...

  5. 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入

    首先分析. 1: 肯定要利用dom4j读取xml配置文件,将所有的bean的配置信息读取出来 2: 利用反射技术,实例化所有的bean 3: 写注解处理器, 利用注解和内省实现依赖对象的注入. 4: ...

  6. Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)

    参见原文:http://www.geekabyte.io/2014/11/difference-between-beanfactory-and.html geekAbyte Codes and Ran ...

  7. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  8. Spring学习--通过注解配置 Bean (三)

    组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...

  9. Spring学习--通过注解配置 Bean (二)

    在 classpath 中扫描组件: 当在组件类上使用了特定的注解之后 , 还需要在 Spring 的配置文件中声明 <context:component-scan>: base-pack ...

随机推荐

  1. go的常用数据类型-持续优化篇

    p.p1 { margin: 0; font: 12px "Helvetica Neue"; color: rgba(69, 69, 69, 1) } p.p2 { margin: ...

  2. mybatis-plus查询指定字段

    show me the code :mybais-plus版本:3.1.1 1,排除某些字段,可以同时排除多个字段排除多个字段写法: .setEntity(new User()) .select(c ...

  3. Effective C++ 总结笔记(三)

    三.资源管理 13.以对象管理资源 1.为了防止资源泄漏,请使用RAII对象,在构造函数里面获得资源,并在析构函数里面释放资源. 2. 引用计数型智慧指针(RCSP):持续追踪多少个指针指向该资源,无 ...

  4. 要web开发精品教程吗?免费无广告一百期连讲的那种-逐浪CMS前端开发100期入门教程全面开放

    要web开发精品教程吗?免费无广告一百期连讲的那种-逐浪CMS前端开发100期入门教程全面开放 大师主讲 经验难得 由逐浪CMS首席架构师发哥老师,亲自主理讲解. 历时一年精心打造, 汇聚了互联网诞生 ...

  5. 「期末」一文带你系统回顾C 语言

    超详细 c 语言回顾 前言 c 语言是一种底层语言,是一种系统底层级的语言,例如Windows.Linux.Unix等操作系统就是使用c语言编写的.所以由此看来,不论是火爆了25年的Java,还是近年 ...

  6. WPF嵌入Unity3D之后,unity3D程序的键盘和鼠标事件无法触发(3D程序的焦点无法激活)的解决方案

    目前最通用的客户端调用3D的方式,就是WPF程序通过Process启动Unity3D的exe进程,直接上代码: //开启3D进程 internal void Create3DProcess(strin ...

  7. AOP实现方式二

    applicationContext.xml <!--方法二 自定义类--> <bean id="diyPointCut" class="com.sha ...

  8. HCNP Routing&Switching之组播技术-组播基础

    组播技术背景 随着internet网络的不断发展,网络中交互的各种数据.语音.视频信息数量突增:新型的在线直播.网络电视.视频会议等应用也在逐渐兴起:这些业务大多符合点到多点的模式,对信息安全性.传播 ...

  9. 【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    问题描述 App Service使用jdbc连接MySQL服务,出现大量的  Communications link failure: com.mysql.cj.jdbc.exceptions.Com ...

  10. 【Tool】Node.js 安装

    Node.js 安装 2019-07-29  14:56:14  by冲冲 1. 说明 电脑使用node.js存在两种方式: ① 第一种,下载(.exe)到本地电脑,不需要安装,下载后点击 node. ...