ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot系列19: 项目启动时初始化资源其他方式

2022-01-13 20:01:37  阅读:149  来源: 互联网

标签:__ 初始化 springboot 19 PostConstruct start 注解 ApplicationListener


       上一篇介绍了通过实现 CommandLineRunner 接口类,实现 run() 方法来实现springboot项目启动时初始化资源,同时 @Order 注解的实现类最先执行,并且@Order()里面的值越小启动越早的特点。有网友留言说可以用其他方式来实现初始化资源的问题,第一种:实现 ApplicationListener 接口,另外一种 @PostConstruct 来实现,今晚就用这两种方式尝试下。

 

第一种,实现 ApplicationListener 接口

/**
 * @version 1.0
 * @description: 监听应用事件
 * @date 2020-09-16 1:22
 */
@Component
public class ApplicationListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent  contextRefreshedEvent) {
 
        System.out.println("The ApplicationListenerConfig to start.");

    }
}

 

执行结果:

Connected to the target VM, address: '127.0.0.1:5265', transport: 'socket'
The service to start

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2020-09-16 01:52:22.700  INFO 7712 --- [           main] top.zlcxy.blog.ApplicationListener       : Starting ApplicationListener on zhangliang with PID 7712 (E:\mpweixinworkspace\springboot-parent\springboot-listener\target\classes started by Administrator in E:\mpweixinworkspace\springboot-parent)
2020-09-16 01:52:22.705  INFO 7712 --- [           main] top.zlcxy.blog.ApplicationListener       : No active profile set, falling back to default profiles: default
The ApplicationListenerConfig to start.
2020-09-16 01:52:23.458  INFO 7712 --- [           main] top.zlcxy.blog.ApplicationListener       : Started ApplicationListener in 1.258 seconds (JVM running for 1.777)

 

通过控制台,可以看出启动项目,确实是执行了初始化代码。

 

第二种:使用注解:`@PostConstruct`

/**
 * @version 1.0
 * @description: PostConstruct注解初始化资源
 * @date 2020-09-16 1:29
 */
@Component
public class PostConstructConfig {

    @PostConstruct
    public void init1() {
        System.out.println("The PostConstructConfig init1 to start.");
    }

    @PostConstruct
    public void init2() {
        System.out.println("The PostConstructConfig init2 to start.");
    }
}

 

运行结果如下:

The service to start

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2020-09-16 02:02:44.466  INFO 6620 --- [           main] top.zlcxy.blog.ApplicationListener       : Starting ApplicationListener on zhangliang with PID 6620 (E:\mpweixinworkspace\springboot-parent\springboot-listener\target\classes started by Administrator in E:\mpweixinworkspace\springboot-parent)
2020-09-16 02:02:44.472  INFO 6620 --- [           main] top.zlcxy.blog.ApplicationListener       : No active profile set, falling back to default profiles: default
The PostConstructConfig init2 to start.
The PostConstructConfig init1 to start.
The ApplicationListenerConfig to start.

 

可以看出控制台,PostConstructConfig  执行了初始化工作。

 

查询了下@PostConstruct 注解详解,供大家学习。

 

简介

    javaEE5引入了@PostConstruct和@PreDestroy两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作。

使用场景

    在项目中主要是在Servlet初始化之前加载一些缓存数据等

API使用说明

    PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用 PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。应用 PostConstruct 注释的方法必须遵守以下所有标准:该方法不得有任何参数,除非是在 EJB 拦截器 (interceptor) 的情况下,根据 EJB 规范的定义,在这种情况下它将带有一个 InvocationContext 对象 ;该方法的返回类型必须为 void;该方法不得抛出已检查异常;应用 PostConstruct 的方法可以是 public、protected、package private 或 private;除了应用程序客户端之外,该方法不能是 static;该方法可以是 final;如果该方法抛出未检查异常,那么不得将类放入服务中,除非是能够处理异常并可从中恢复的 EJB。

特点:

1、只有一个非静态方法能使用此注解

2、被注解的方法不得有任何参数

3、被注解的方法返回值必须为void

4、被注解方法不得抛出已检查异常

5、此方法只会被执行一次

 

servlet执行流程

 

        

 

注意事项

    使用此注解时会影响服务启动时间。服务启动时会扫描WEB-INF/classes的所有文件和WEB-INF/lib下的所有jar包。

以上网友说的这两种方式项目启动时初始化资源都是可行的,但从初始化执行顺序角度来说,还是上一篇介绍的方法实用,您觉得呢?

 

标签:__,初始化,springboot,19,PostConstruct,start,注解,ApplicationListener
来源: https://www.cnblogs.com/yb-ken/p/15799445.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有