@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注解详解的更多相关文章

  1. Spring系列20:注解详解和Spring注解增强(基础内功)

    有部分小伙伴反馈说前面基于注解的Spring中大量使用注解,由于对Java的注解不熟悉,有点难受.建议总结一篇的Java注解的基础知识,那么,它来了! 本文内容 什么是注解? 如何定义注解 如何使用注 ...

  2. spring @Resource与@Autowired注解详解

    具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...

  3. spring+hibernate实体类注解详解(非原创) + cascade属性取值

    @Entity //继承策略.另一个类继承本类,那么本类里的属性应用到另一个类中 @Inheritance(strategy = InheritanceType.JOINED ) @Table(nam ...

  4. Spring IoC 公共注解详解

    前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 什么是公共注解?公共注解就是常见的Java ...

  5. Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  6. Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!

    点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...

  7. Spring Boot 之使用 Json 详解

    Spring Boot 之使用 Json 详解 简介 Spring Boot 支持的 Json 库 Spring Web 中的序列化.反序列化 指定类的 Json 序列化.反序列化 @JsonTest ...

  8. @RequestBody, @ResponseBody 注解详解(转)

    原文地址: https://www.cnblogs.com/qq78292959/p/3760651.html @RequestBody, @ResponseBody 注解详解(转) 引言: 接上一篇 ...

  9. 【spring源码学习】Spring @PostConstruct和@PreDestroy实例

    在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...

随机推荐

  1. Let's Code

    Let's Code Happy Coding, Happy OI #include <bits/stdc++.h> using namespace std; int main() { c ...

  2. windows系统 MySQL8.0.12详细安装步骤及基本使用教程

    转载  https://blog.csdn.net/xiezhiming1234/article/details/82860339

  3. 怎样设置HTTP请求头Header

    使用: xhr.setRequestHeader(); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequest ...

  4. 怎样通过id属性值获取元素节点

    方法1: 使用document.getElementById(); 方法2: 使用document.querySelector(); document.getElementById("app ...

  5. springCloud的feign异常:RequestHeader参数为空时,对key加了大括号{}

    好久没写博客了,今天趁着周末把工作中遇到的问题梳理一下(在这个问题排查过程中,发现自己排查问题的能力还是太弱了,需要加强). 最近在公司springCloud的项目里,通过feign远程调用其他服务, ...

  6. 安装笔记, caffe 、 opencv等

    1. 1.1 opencv static linux mkdir build & cd build cmake .. -LH  这句话用来查看编译选项  如果不知道编译啥  可以用这个查看一下 ...

  7. SVG学习之stroke-dasharray 和 stroke-dashoffset 详解

    本文适合对SVG已经有所了解,但是对stoke-dasharray和stroke-dashoffset用法有疑问的童鞋 第一:概念解释 1. stroke意思是:画短线于,在...上划线 2. str ...

  8. python实现蓝牙通信

    安装和示例 linux下安装 -dev sudo pip install bluepy 官方示例 import btle class MyDelegate(btle.DefaultDelegate): ...

  9. ORACLE_笔记_练习题目

    一.plsql用法网址及时复习 extract()函数----用于截取年.月.日.时.分.秒 https://www.cnblogs.com/xqzt/p/4477239.html case when ...

  10. myBatis的坑 01 %的坑 框架内置的小BUG

    <select id="queryUserLikeUserName" resultType="cn.itcast.pojo.User"> selec ...