zoukankan      html  css  js  c++  java
  • 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 正在被容器删除");
    
        }
    
    }
    
  • 相关阅读:
    使用 ServiceStack 构建跨平台 Web 服务
    .NET的微型Web框架 Nancy
    orcale复制表结构及其数据
    利用PL/SQL Developer工具导出数据到excel,导入excel数据到表
    PLSQL导入/导出数据方法
    基于Quqrtz.NET 做的任务调度管理工具
    Web监听器导图详解(转)
    GIT分支管理是一门艺术(转)
    我需要完全理解这部分代码才能确保它能够正常工作,如果由我来修复代码中的问题,我是不会这么写的,因此希望你也不要这么来写(转)
    不要学习代码,要学会思考(转)
  • 原文地址:https://www.cnblogs.com/myblogs-miller/p/9139082.html
Copyright © 2011-2022 走看看