设为首页 加入收藏

TOP

quarkus依赖注入之七:生命周期回调(二)
2023-08-06 07:49:31 】 浏览:85
Tags:quarkus 赖注入 周期回
ain) end AroundConstruct 15:26:33,041 INFO [com.bol.int.imp.LifeCycleInterceptor] (main) life cycle PostConstruct 15:26:33,041 INFO [com.bol.lif.Hello] (main) Hello world! 15:26:33,097 INFO [com.bol.int.imp.LifeCycleInterceptor] (main) life cycle PreDestroy 15:26:33,128 INFO [io.quarkus] (main) Quarkus stopped in 0.075s
  • 以上就是通过拦截器制作的bean生命周期回调的全过程,接下来再看另一种方式:不用拦截器的方式

自定义模式

  • 刚才的拦截器模式有个明显问题:如果不同bean的生命周期回调有不同业务需求,该如何是好?为每个bean做一个拦截器吗?随着bean的增加会有大量拦截器,似乎不是个好的方案
  • 如果您熟悉spring,对下面的代码要改不陌生,这是来自spring官网的内容,直接在bean的方法上用PostConstruct和PreDestroy修饰,即可在bean的创建完成和销毁前被调用
public class CachingMovieLister {

  @PostConstruct
  public void populateMovieCache() {
      // populates the movie cache upon initialization...
  }

  @PreDestroy
  public void clearMovieCache() {
      // clears the movie cache upon destruction...
  }
}
  • 实际上,quarkus也支持上述方式,不过和拦截器相比有两个差异:
  1. 在bean的内部,只能用PostConstruct和TrackLifeCycle,不能用AroundConstruct,只有拦截器才能用AroundConstruct
  2. 在拦截器中,PostConstruct和TrackLifeCycle修饰的方法必须要有InvocationContext类型的入参,但是在bean内部则没有此要求
  • 咱们来改造Hello.java的源码,修改后如下,增加了两个方法,分别被PostConstruct和PreDestroy修饰
@ApplicationScoped
@TrackLifeCycle
public class Hello {

    public Hello() {
        Log.info(this.getClass().getSimpleName() + " at instance");
    }

    @PostConstruct
    public void doPostConstruct() {
        Log.info("at doPostConstruct");
    }

    @PreDestroy
    public void doPreDestroy() {
        Log.info("at PreDestroy");
    }

    public void helloWorld() {
        Log.info("Hello world!");
    }
}
  • 再次运行单元测试,控制台输出如下,可见Hello自定义的两个生命周期回调都执行了,同时原拦截器的三个回调也都正常执行
16:27:54,134 INFO  [io.quarkus] (main) Quarkus 2.7.3.Final on JVM started in 2.529s. Listening on: http://localhost:8081
16:27:54,135 INFO  [io.quarkus] (main) Profile test activated. 
16:27:54,135 INFO  [io.quarkus] (main) Installed features: [agroal, cdi, narayana-jta, resteasy, smallrye-context-propagation, vertx]
16:27:54,147 INFO  [com.bol.lif.Hello] (main) Hello_ClientProxy at instance
16:27:54,710 INFO  [com.bol.int.imp.LifeCycleInterceptor] (main) start AroundConstruct
16:27:54,711 INFO  [com.bol.lif.Hello] (main) Hello_Subclass at instance
16:27:54,711 INFO  [com.bol.int.imp.LifeCycleInterceptor] (main) end AroundConstruct
16:27:54,711 INFO  [com.bol.int.imp.LifeCycleInterceptor] (main) life cycle PostConstruct
16:27:54,712 INFO  [com.bol.lif.Hello] (main) at doPostConstruct
16:27:54,712 INFO  [com.bol.lif.Hello] (main) Hello world!
16:27:54,747 INFO  [com.bol.int.imp.LifeCycleInterceptor] (main) life cycle PreDestroy
16:27:54,747 INFO  [com.bol.lif.Hello] (main) at PreDestroy
16:27:54,765 INFO  [io.quarkus] (main) Quarkus stopped in 0.044s

dispose注解:实现销毁前自定义操作,dispose是另一种可选方案

  • 试想这样的场景:我的bean在销毁前要做自定义操作,但是如果用之前的两种方案,可能面临以下问题:
  1. 不适合修改bean的代码,bean的类可能是第三方库
  2. 也不适合修改生命周期拦截器代码,拦截器可能也是第三方库,也可能是多个bean共用,若修改会影响其他bean
  • 好在quarkus为我们提供了另一个方案,不用修改bean和拦截器的代码,用注解dispose修饰指定方法即可,接下来编码验证
  • 增加一个普通类ResourceManager.java,假设这是业务中的资源管理服务,可以打开和关闭业务资源,稍后会在配置类中将其指定为bean
package com.bolingcavalry.service.impl;

import io.quarkus.logging.Log;

/**
 * @author zq2599@gmail.com
 * @Title: 资源管理类
 * @Package
 * @Description:
 * @date 4/10/22 10:20 AM
 */
public class ResourceManager {

    public ResourceManager () {
        Log.info("create instance, " + this.getClass().get
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JAVA SE基础《八》 ---- 面对对象.. 下一篇6、Spring之基于xml的自动装配

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目