Springboot最佳实践:在Spring Boot启动时添加方法运行(下)

简介: Springboot最佳实践:在Spring Boot启动时添加方法运行

3. Spring Boot中的应用程序事件

Spring框架在不同情况下触发不同事件。它还会在启动过程中触发许多事件。我们可以使用这些事件来执行代码,例如,在Spring Boot应用程序启动后,可以使用ApplicationReadyEvent执行代码。

如果我们不需要命令行参数,这是在应用程序启动后执行代码的最佳方法。

@Component
public class RunAfterStartup{
@EventListener(ApplicationReadyEvent.class)
public void runAfterStartup() {
    System.out.println("Yaaah, I am running........");
}

输出:

Yaaah, I am running........

春季靴子中最重要的事件是

  • ApplicationContextInitializedEvent :在准备ApplicationContext并调用ApplicationContextInitializers之后但在加载bean定义之前触发
  • ApplicationPreparedEvent :在加载bean定义后触发
  • ApplicationStartedEvent :在刷新上下文之后但在调用命令行和应用程序运行程序之前触发
  • ApplicationReadyEvent :在调用任何应用程序和命令行运行程序之后触发
  • ApplicationFailedEvent :如果启动时发生异常则触发

可以创建多个ApplicationListeners。可以使用@Order批注或Ordered接口对其进行订购。

该顺序与其他相同类型的ApplicationListener共享,但不与ApplicationRunners或CommandLineRunners共享。

4.方法上的@Postconstruct注解

可以使用@PostConstruct批注标记方法。每当使用此注释标记方法时,将在依赖项注入后立即调用该方法。

@PostConstruct方法链接到特定的类,因此它仅应用于特定于类的代码。每个类只有一个带有postConstruct批注的方法。

@Component
public class PostContructImpl {
    public PostContructImpl() {
        System.out.println("PostContructImpl Constructor called");
    }
    @PostConstruct
    public void runAfterObjectCreated() {
        System.out.println("PostContruct method called");
    }
}

输出:

PostContructImpl Constructor called
postContruct method called

需要注意的是,如果class标记为lazy,则意味着在请求时创建了class。之后,将执行标有@postConstruct批注的方法。

标有postConstruct批注的方法可以具有任何名称,但是不能具有任何参数。它必须是无效的,并且不能是静态的。

请注意,@postConstruct批注是Java EE模块的一部分,在Java 9中被标记为已弃用,在Java 11中已被删除。我们仍然可以通过将java.se.ee添加到应用程序中来使用它。

5. InitializingBean接口

InitializingBean解决方案的工作原理与postConstruct批注完全相似。不必使用注释,我们必须实现InitializingBean接口。然后,我们需要重写afterPropertiesSet()方法。

InitializingBean是org.springframework.beans.factory包的一部分。

@Component
public class InitializingBeanImpl implements InitializingBean {
    public InitializingBeanImpl() {
        System.out.println("InitializingBeanImpl Constructor called");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBeanImpl afterPropertiesSet method called");
    }
}

您必须考虑如果同时使用@PostConstruct批注和InitializingBean会发生什么。那么在这种情况下,@PostConstruct方法将在InitializingBean的afterPropertiesSet()方法之前调用。

6. @bean批注的init属性

我们可以在@Bean批注中使用initMethod属性提供一种方法。bean初始化后将调用此方法。

initMethod中提供的方法必须为空,并且不能包含任何参数。此方法甚至可以是私有的。

public class BeanInitMethodImpl {
    public void runAfterObjectCreated() {
        System.out.println("yooooooooo......... someone called me");
    }
}
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Bean(initMethod="runAfterObjectCreated")
    public BeanInitMethodImpl getFunnyBean() {
        return new BeanInitMethodImpl();
    }
 }

输出:

yooooooooo......... someone called me

如果您具有同一类的InitializingBean实现和@Bean批注的initMethod属性,则将initMethod之前调用InitializingBean的afterPropertiesSet方法。

结合不同的方法:

最后,有时我们可能需要组合多个选项。然后,它们将按照以下顺序执行:

  • 建设者
  • 后构造方法
  • afterPropertiesSet方法
  • Bean初始化方法
  • ApplicationStartedEvent
  • ApplicationRunner或CommandLineRunner取决于顺序
  • ApplicationReadyEvent

快速阅读

  • 春季启动应用程序启动后,有多种方法可以运行代码
  • 我们可以使用CommandLineRunner或ApplicationRunner接口
  • 使用ApplicationRunner接口访问已解析的参数,而不是原始字符串数组
  • Spring Boot事件在应用程序启动时执行代码
  • 标有@PostConstruct批注的方法在对象初始化后执行
  • 对象初始化之后调用InitializingBean接口的afterPropertiesSet()方法
  • @Bean批注具有属性“ initMethod”以提供将在bean初始化后调用的方法

相关话题

  • Spring Boot项目设置指南
目录
相关文章
|
8天前
|
Java 关系型数据库 MySQL
【Java Spring开源项目】新蜂(NeeBee)商城项目运行、分析、总结
【Java Spring开源项目】新蜂(NeeBee)商城项目运行、分析、总结
159 4
|
8天前
|
移动开发 前端开发 NoSQL
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
144 0
|
8天前
|
XML Java 数据库连接
Spring框架与Spring Boot的区别和联系
Spring框架与Spring Boot的区别和联系
29 0
|
8天前
|
Java Spring
Spring的@Retryable实现方法重试
`@Retryable`注解用于配置异常重试,参数包括:指定异常类型`value`,额外包含异常`include`,排除异常`exclude`,最大尝试次数`maxAttempts`和回退策略`backoff`。可选地,可以用`retryExceptions`列表替换`value`。当重试失败,可使用`@Recover`注解定义恢复逻辑。
20 1
|
8天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
22 2
|
8天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
8天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
8天前
|
Java Maven Docker
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
33 2
|
Java 应用服务中间件 Maven
传统maven项目和现在spring boot项目的区别
Spring Boot:传统 Web 项目与采用 Spring Boot 项目区别
370 0
传统maven项目和现在spring boot项目的区别
|
XML Java 数据库连接
创建springboot项目的基本流程——以宠物类别为例
创建springboot项目的基本流程——以宠物类别为例
129 0
创建springboot项目的基本流程——以宠物类别为例