项目启动时socket自动启动_22springboot系列: 项目启动时初始化资源其他方式

本文介绍了两种SpringBoot项目启动时初始化资源的方式:实现ApplicationRunner接口和使用@PostConstruct注解。分别展示了其实现方法、执行顺序、特点和注意事项,指出ApplicationRunner可能更实用。
摘要由CSDN通过智能技术生成

a02d94e46325f8e21bd1c0b7066fbb64.gif

b5cdd9475a7be99f80e2abe924c4da96.png

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

第一种,实现 ApplicationListener 接口 

/** * @author zhangl * @version 1.0 * @description: 监听应用事件 * @date 2020-09-16 1:22 */@Componentpublic 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: defaultThe 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`

/** * @author zhangl * @version 1.0 * @description: PostConstruct注解初始化资源 * @date 2020-09-16 1:29 */@Componentpublic 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: defaultThe 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执行流程

                                             3ab6935a885dcb7329dec8fb21aae07f.png

注意事项

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

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

源码获取:

关注公众号,输入"springboot-parent"获取git地址

历史回顾:

[20200613] 17-springboot系列:单数据源MongoDB

[20200614] 18-springboot系列:多数据源MongoDB

[20200616] 19-springboot系列:文件上传

[20200701] 20-springboot系列:MyBatis-Plus整合使用

[20200915] 21-springboot系列:CommandLineRunner解决项目启动时初始化资源

作者微信号:13128600812

加入技术群讨论,备注:1

软件定制及其他业务,备注:2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值