Spring@PostConstruct和@PreDestroy注解详解
@PostConstruct注解使用
@PostConstructApi使用说明
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation
PostConstruct批注用于需要依赖注入完成以执行任何初始化之后需要执行的方法上。在类投入使用之前必须调用此方法。所有支持依赖注入的类都必须支持该注释。即使该类不要求注入任何资源,也必须调用用PostConstruct注释的方法。此注释只能注释一种方法
@PostConstruct的注意事项有:
除了拦截器的情况外,该方法不得具有任何参数,在这种情况下,该方法将采用Interceptors规范定义的InvocationContext对象
在拦截器里定义的方法必须满足下面两种方式
- void (InvocationContext)
- Object (InvocationContext) throws Exception
在非拦截器里定义的方法必须满足这种形式
- void ()
应用PostConstruct的方法可以用public,protected,package private 或private 修饰。
除了应用程序客户端,该方法一定不能是静态的
方法可能是用final修饰的
如果该方法抛出未经检查的异常,则该类不得投入使用,除非在EJB可以处理异常甚至从异常中恢复的EJB情况下
SpringBean 的初始化流程
st=>start: 开始
op1=>operation: Spring容器加载
op2=>operation: 调用构造函数
op3=>operation: @PostConstruct方法调用
op4=>operation: init()调用
op5=>operation: 其他代码
op6=>operation: destroy()调用
op7=>operation: @PreDestroy方法调用
e=>end: 结束
st->op1->op2->op3->op4->op5->op6->op7->e
@PostConstruct方法是在init()方法之前构造方法调用之后执行的
项目应用场景
一般用于一些系统配置或者缓存数据的加载。
@PreDestroy注解
The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with PreDestroy is typically used to release resources that it has been holding. This annotation MUST be supported by all container managed objects that support PostConstruct except the application client container in Java EE 5
@PreDestroy 注解在方法上用作回调通知,以表明实例正在被容器删除。带有@PreDestroy 注解的方法通常用于释放它一直持有的资源。除Java EE 5中的应用程序客户端容器外,所有支持PostConstruct的容器管理对象都必须支持此注释。
@PreDestroy的注意事项有:
除了拦截器的情况外,该方法不得具有任何参数,在这种情况下,该方法将采用Interceptors规范定义的InvocationContext对象
在拦截器里定义的方法必须满足下面两种方式
- void (InvocationContext)
- Object (InvocationContext) throws Exception
在非拦截器里定义的方法必须满足这种形式
- void ()
应用@PreDestroy注解的方法可以用public,protected,package private 或private 修饰。
该方法一定不能是静态的
方法可能是用final修饰的
如果该方法抛出未经检查的异常,则该类不得投入使用,除非在EJB可以处理异常甚至从异常中恢复的EJB情况下
使用实例:
public Class XXX {
@Autowired
private YYY y;
public XXX() {
System.out.println("此时y还未被注入: y = " + y);
}
@PostConstruct
private void init() {
System.out.println("@PostConstruct将在依赖注入完成后被自动调用: y = " + y);
}
@Predestory
private void preDestory() {
System.out.println("XXX 正在被容器删除");
}
}
Spring@PostConstruct和@PreDestroy注解详解的更多相关文章
- Spring系列20:注解详解和Spring注解增强(基础内功)
有部分小伙伴反馈说前面基于注解的Spring中大量使用注解,由于对Java的注解不熟悉,有点难受.建议总结一篇的Java注解的基础知识,那么,它来了! 本文内容 什么是注解? 如何定义注解 如何使用注 ...
- spring @Resource与@Autowired注解详解
具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...
- spring+hibernate实体类注解详解(非原创) + cascade属性取值
@Entity //继承策略.另一个类继承本类,那么本类里的属性应用到另一个类中 @Inheritance(strategy = InheritanceType.JOINED ) @Table(nam ...
- Spring IoC 公共注解详解
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 什么是公共注解?公共注解就是常见的Java ...
- Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!
点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...
- Spring Boot 之使用 Json 详解
Spring Boot 之使用 Json 详解 简介 Spring Boot 支持的 Json 库 Spring Web 中的序列化.反序列化 指定类的 Json 序列化.反序列化 @JsonTest ...
- @RequestBody, @ResponseBody 注解详解(转)
原文地址: https://www.cnblogs.com/qq78292959/p/3760651.html @RequestBody, @ResponseBody 注解详解(转) 引言: 接上一篇 ...
- 【spring源码学习】Spring @PostConstruct和@PreDestroy实例
在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...
随机推荐
- JDBC 资源绑定器 ,处理查询结果集
使用资源绑定器绑定属性配置 实际开发中不建议把连接数据库的信息写死到Java程序中 //使用资源绑定器绑定属性配置 ResourceBundle bundle = ResourceBundle.get ...
- Java面试--类加载顺序
类什么时候就行初始化: 1)创建类的实例,也就是new一个对象 2)访问某个类或接口的静态变量,或者对该静态变量赋值 3)调用类的静态方法 4)反射(Class.forName(“com.fan ...
- 2019牛客暑期多校训练营(第三场)F Planting Trees 单调队列
F Planting Trees 题目链接 https://ac.nowcoder.com/acm/contest/883/F 题目描述 The semester is finally over an ...
- LC 349. Intersection of Two Arrays
题目描述 Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1, ...
- python学习-9 pycharm的安装
1.python 开发IDE : pycharm.eclipse等等 安装: 百度搜索pycharm 就可以了,去官网下载专业版.(百度有各种破解方法) #不要 ...
- 用函数来编写实现strlen()函数功能
strlen( )函数: 测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0’) //strlen( )函数:测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0 ...
- 怎样判断浏览器是否支持canvas
1. 如果网页必须使用canvas, 则需要告知用户更换或更新浏览器. 这时可以通过在<canvas>标签之间添加替代元素进行 <canvas id="c1"&g ...
- Idea加载项目扫描完毕后自动退出
问题描述:Idea平时好好的,突然就打开后扫描完毕后自动退出.网上说修改idea.exe.vmoptions文件的Xmx,还是不行. 后来根据http://www.pianshen.com/artic ...
- lesson10总结
package lesson10; public class Fa { String name="I am Fa"; static{ System.out.println(&qu ...
- 给枚举定义DescriptionAttribute
在C#中,枚举用来定状态值很方便,例如我定义一个叫做Season的枚举 public enum Season { Spring = 1, Summer = 2, Autumn = 3, Winter ...