当前位置: 首页 > wzjs >正文

国家建设安全局网站广州疫情今天最新消息

国家建设安全局网站,广州疫情今天最新消息,公司网站建设要求,wordpress淘宝客开发在 Spring Boot 中,初始化方法通常是在应用程序启动时被调用的,可以用来执行应用启动时的一些准备工作。以下是几种常见的初始化方法: 一、顺序 1. 图解 ┌─────────────────────────────┐│ Spring Boot…

在 Spring Boot 中,初始化方法通常是在应用程序启动时被调用的,可以用来执行应用启动时的一些准备工作。以下是几种常见的初始化方法:

一、顺序

1. 图解

                ┌─────────────────────────────┐│    Spring Boot启动           │└─────────────────────────────┘│▼┌─────────────────────────────┐│     Spring Application      ││    Context 加载和注入         │└─────────────────────────────┘│▼┌─────────────────────────────┐│     依赖注入完成              ││       (`@PostConstruct`)    │└─────────────────────────────┘│▼┌─────────────────────────────┐│      `InitializingBean`     ││   调用 `afterPropertiesSet()`│└─────────────────────────────┘│▼┌─────────────────────────────┐│      初始化 `@Bean`          ││    (配置类中 `@Bean` 方法)    │└─────────────────────────────┘│▼┌─────────────────────────────┐│   `CommandLineRunner`       ││   和 `ApplicationRunner`    │└─────────────────────────────┘│▼┌─────────────────────────────┐│   触发应用事件                ││   (`ApplicationListener`)   │└─────────────────────────────┘│▼┌─────────────────────────────┐│     应用启动完成              ││  (`ApplicationReadyEvent`)  │└─────────────────────────────┘

2. 执行顺序过程

  1. Spring Boot 启动 (SpringApplication.run())
    这是应用启动的入口,它会初始化 Spring 上下文,加载所有 Bean 配置等。

  2. 依赖注入完成后 (@PostConstruct / InitializingBean)
    @PostConstruct 在所有依赖注入完成后立即执行,用于执行一些初始化工作。
    InitializingBean 的 afterPropertiesSet() 方法也在所有依赖注入完成后执行,通常用于进行一些逻辑初始化。

  3. 初始化 @Bean 方法
    Spring 会在容器启动过程中调用配置类中的 @Bean 方法进行 Bean 初始化。这些方法通常用于返回一些 Spring 管理的 Bean。

  4. CommandLineRunner 和 ApplicationRunner 执行
    这些接口的 run() 方法会在 Spring Boot 完全初始化后执行,通常用于执行一些在应用启动完成后的操作。

  5. ApplicationListener 监听器触发
    ApplicationListener 可以监听 ApplicationReadyEvent 事件等,这个事件会在应用完全启动并准备就绪后触发。

  6. 应用启动完成 (ApplicationReadyEvent)
    当应用启动并准备好后,ApplicationReadyEvent 会被触发。此时,应用就可以接受请求或执行其他操作了。

二、方法详解

1. @PostConstruct 注解

@PostConstruct 注解用于标注一个方法,表示在 Spring 完成所有依赖注入后,会调用该方法。这通常用于类初始化时执行的操作。

@Component
public class MyService {@PostConstructpublic void init() {System.out.println("Initializing MyService...");}
}

2. CommandLineRunner 接口

CommandLineRunner 是 Spring Boot 提供的一个接口,它的 run 方法会在应用启动完成后执行。CommandLineRunner 接口允许开发者在应用启动时执行一些特定的逻辑。

@SpringBootApplication
public class MyApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}@Overridepublic void run(String... args) throws Exception {System.out.println("Application started with CommandLineRunner...");}
}

3. ApplicationRunner 接口

ApplicationRunner 接口与 CommandLineRunner 类似,区别在于它提供了一个 ApplicationArguments 参数,可以获取启动时传入的命令行参数。

@SpringBootApplication
public class MyApplication implements ApplicationRunner {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("Application started with ApplicationRunner...");System.out.println("Non-option arguments: " + args.getNonOptionArgs());}
}

4. SpringApplication 类的 run() 方法

Spring Boot 的 SpringApplication 类的 run() 方法是应用启动的入口,它可以配置一些初始化行为,如设置应用的环境、启用配置文件等。

public static void main(String[] args) {SpringApplication app = new SpringApplication(MyApplication.class);app.setAdditionalProfiles("dev");app.run(args);
}

5. @Configuration 配置类中的 @Bean 方法

在配置类中,通过 @Bean 注解定义的 Bean 方法,会在应用启动时被执行。

@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyService();}
}

6. ApplicationListener 监听器

ApplicationListener 可以监听 Spring Boot 应用的启动事件,例如 ContextRefreshedEventApplicationReadyEvent,这些事件可以用于执行初始化逻辑。

@Component
public class AppStartupListener implements ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {System.out.println("Application is ready!");}
}

7. @Bean 注解与 @PostConstruct 配合

如果你在 Spring 配置类中使用 @Bean 注解创建一个 Bean,并且需要在该 Bean 被创建后进行一些初始化操作,可以将 @PostConstruct 注解添加到 Bean 类中的初始化方法。

@Configuration
public class MyConfig {@Beanpublic MyService myService() {return new MyService();}@PostConstructpublic void setup() {System.out.println("Initializing configuration...");}
}

8. InitializingBean 接口

InitializingBean 是 Spring 提供的一个接口,允许开发者在 Bean 被初始化之后执行特定的操作。这个接口与 @PostConstruct 注解有些类似,但 InitializingBean 是一个更传统的方式,适用于较早的 Spring 版本,而 @PostConstruct 是 Java EE 标准的一部分。InitializingBean 接口的主要方法是 afterPropertiesSet(),它会在 Spring 容器完成依赖注入并且所有 Bean 属性都设置好之后执行。也就是说,afterPropertiesSet() 方法在 Bean 初始化时被调用,通常用于执行一些与 Bean 相关的初始化工作。

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;@Component
public class MyService implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {// 执行初始化操作System.out.println("MyService has been initialized!");}
}

执行顺序

在 Spring 中,InitializingBeanafterPropertiesSet() 方法的调用顺序如下:

  1. 依赖注入完成后:Spring 完成对所有 Bean 的依赖注入,确保所有的属性都被正确设置。
  2. InitializingBeanafterPropertiesSet() 被调用:Spring 会依次调用实现了 InitializingBean 接口的 Bean 的 afterPropertiesSet() 方法。
  3. @PostConstruct 注解方法被调用:如果 @PostConstruct 方法存在,它将在 afterPropertiesSet() 后调用。

InitializingBean@PostConstruct 的区别

特性InitializingBean@PostConstruct
接口/注解接口 (InitializingBean)注解 (@PostConstruct)
调用时机在依赖注入完成之后调用 (afterPropertiesSet())在依赖注入完成之后调用(容器初始化后,通常早于 afterPropertiesSet()
使用范围需要实现 InitializingBean 接口可以标注在任何一个方法上
适用场景适用于老版本的 Spring 或者需要控制初始化逻辑的情况适用于更简洁的场景,推荐用于标注简单初始化方法
@Bean 配合可以与配置类中的 @Bean 配合,调用 afterPropertiesSet()一般情况下,@PostConstruct 用于实例化 Bean 时的初始化方法
http://www.dtcms.com/wzjs/510631.html

相关文章:

  • 网站制作2019趋势宁德市旅游景点大全
  • 企业网站优化怎么提高关键词排名seo网络推广教程
  • 深圳网站建设公司联系百度推广如何获取精准的客户
  • 一个人做b2b2c网站网站优化排名方案
  • wordpress如何设置内容页搜索引擎关键词优化
  • 登建设厅锁子的是哪个网站国内最近的新闻大事
  • 网站编程学习泰安短视频seo
  • 新公司做网站怎么弄今日头条收录入口
  • 秦皇岛高端网站设计收录网站查询
  • 政府网站管建设情况汇报百度推广怎么做免费
  • 企业网站建设杭州公司佛山seo关键词排名
  • 做宣传网站买什么云服务器百度游戏客服在线咨询
  • 徐州便民信息网优化seo教程技术
  • 如何再网站上做免费广告厦门网站关键词推广
  • 济宁做网站哪家比较好seo少女
  • 域名商的网站每日军事新闻
  • 还有哪些免费的网站可以做H5信息流优化师
  • 如何用模板做公司网站seo薪酬水平
  • wordpress主题point谷歌seo网站建设
  • 网站建设广告词品牌推广营销
  • wordpress网站模板仿站工具百度搜索排行榜前十名
  • 网站架构图怎么做今天的国际新闻
  • 番禺人才网最新招聘信息seo优化技术是什么
  • 青岛网站开发招聘下载谷歌浏览器
  • 新品牌推广策略厦门seo搜索排名
  • 陇城科技网站建设seo网站优化软件
  • 樟木头仿做网站互联网营销行业前景
  • 自动发卡网站怎么做广州专业网络推广公司
  • 温州在线课堂seo短视频
  • 网站logo图怎么做的长沙网络营销公司