【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁。
Spring中声明的Bean的初始化和销毁方法有3种方式:
@Bean的注解的initMethod、DestroyMethod属性
bean实现InitializingBean、DisposableBean接口
@PostConstruct、@PreDestroy注解
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
是一个接口,这个接口中的postProcessBeforeInitialization
、postProcessAfterInitialization
方法分别在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的初始化、销毁方法的方式的更多相关文章
- Spring Framework核心概念之Bean生命周期管理
目录 Spring Bean的生命周期 相关接口的分类 测试SpringBean生命周期的Demo程序 小结 Spring Bean的生命周期 Spring容器既Application或者WebApp ...
- Spring bean 实现初始化、销毁方法的方式及顺序
Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...
- Spring之使用注解实例化Bean并注入属性
1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...
- Spring中用@DependsOn注解控制Bean的创建顺序
1. 概述 Spirng容器自己会管理bean的生命周期和bean实例化的顺序,但是我们仍然可以根据我们自己的需求进行定制.我可以可以选择使用SmartLifeCycle接口,也可以用@Depends ...
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
首先分析. 1: 肯定要利用dom4j读取xml配置文件,将所有的bean的配置信息读取出来 2: 利用反射技术,实例化所有的bean 3: 写注解处理器, 利用注解和内省实现依赖对象的注入. 4: ...
- 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 ...
- 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案
问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...
- Spring学习--通过注解配置 Bean (三)
组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...
- Spring学习--通过注解配置 Bean (二)
在 classpath 中扫描组件: 当在组件类上使用了特定的注解之后 , 还需要在 Spring 的配置文件中声明 <context:component-scan>: base-pack ...
随机推荐
- Java——去掉小数点后面多余的0
当小数点后位数过多,多余的0没有实际意义,根据业务需求需要去掉多余的0.后端存储浮点型数据一般会用到Bigdecimal 类型,可以调用相关方法去掉小数后多余0,然后转为string. public ...
- 《手把手教你》系列技巧篇(四十一)-java+ selenium自动化测试 - 处理iframe -上篇(详解教程)
1.简介 原估计宏哥这里就不对iframe这个知识点做介绍和讲解了,因为前边的窗口切换就为这种网页处理提供了思路,另一个原因就是虽然iframe很强大,但是现在很少有网站用它了.但是还是有小伙伴或者童 ...
- CODING添加ssh提示格式错误的问题
不能去.shh文件夹打开id_rsa.pub文件查看 解决方法: 进入.ssh文件夹,然后右键git bash here 输入代码 cat id_rsa.pub 回车即可
- Kubernetes 中的 gRPC 负载均衡
安装环境依赖 docker-desktop >= 4.1.1 kubernetes >= 1.21.5 go >= 1.17 protobuf >= 3.17.3 istioc ...
- CSS学习笔记:grid布局
目录 一.Grid布局简介 二.Grid布局的一些概念 三. 容器元素属性 1. grid-template-* 1.1 网格行和列的设置 1.2 repeat的使用 1.3 使用fr 1.4 aut ...
- [bzoj5510]唱跳rap和篮球
显然答案可以理解为有(不是仅有)0对情况-1对情况+2对情况-- 考虑这个怎么计算,先计算这t对情况的位置,有c(n-3t,t)种情况(可以理解为将这4个点缩为1个,然后再从中选t个位置),然后相当于 ...
- go语言并发编程
引言 说到go语言最厉害的是什么就不得不提到并发,并发是什么?,与并发相关的并行又是什么? 并发:同一时间段内执行多个任务 并行:同一时刻执行多个任务 进程.线程与协程 进程: 进程是具有一定独立功能 ...
- SpringMVC学习笔记---依赖配置和简单案例实现
初识SpringMVC 实现步骤: 新建一个web项目 导入相关jar包 编写web.xml,注册DispatcherServlet 编写springmvc配置文件 接下来就是去创建对应的控制类 , ...
- c# Quartzs定时器的简单使用
使用背景: 首先想到倒计时,定时任务.大家想到的肯定就是定时器.那么定时器在web和winfrom程序中有着很大的作用.那在服务器端有没有像定时器一样的存在呢. 有了这些需求,我找到了第三方的组件 Q ...
- AtCoder Grand Contest 055 题解
A 赛时直到最后 10min 才做出这个 A 题,之前猜了一个结论一直没敢写,本来不抱啥希望 AC 的结果比赛结束时交了一发竟然 A 了,由此可见我的水平之菜/dk 考虑每次取出字符串开头字符,不妨设 ...