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

前端做网站需要学什么软件做公司网站需要多少钱

前端做网站需要学什么软件,做公司网站需要多少钱,精品课程网站建设项目验收单,网站备案换主体1. SpringBoot启动后自动执行方法的各种方式 1.1 PostConstruct 注解 作用:在依赖注入完成后执行初始化方法。 适用场景:需要在Bean初始化时执行某些操作(如配置、预加载数据)。 注意:该方法在Bean初始化阶段执行&…

1. SpringBoot启动后自动执行方法的各种方式

1.1 @PostConstruct 注解

作用:在依赖注入完成后执行初始化方法。

适用场景:需要在Bean初始化时执行某些操作(如配置、预加载数据)。

注意:该方法在Bean初始化阶段执行,此时应用可能未完全启动(如数据库连接未就绪)。

示例代码:

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;@Component
public class MyPostConstructBean {@PostConstructpublic void init() {System.out.println("PostConstruct 方法执行");// 执行初始化逻辑}
}

1.2. 实现 InitializingBean 接口

作用:通过重写 afterPropertiesSet 方法实现初始化。

适用场景:需要在属性注入后执行操作,与 @PostConstruct 类似但需实现接口。

示例代码:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;@Component
public class MyInitializingBean implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitializingBean 的 afterPropertiesSet 方法执行");// 执行初始化逻辑}
}

1.3. 实现 ApplicationRunner 接口

作用:在Spring应用上下文刷新后执行,支持接收命令行参数。

适用场景:需要在应用启动完成后执行操作,此时Bean已初始化完毕。

示例代码:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner 执行");// 执行逻辑,可访问命令行参数 args}
}

1.4. 实现 CommandLineRunner 接口

作用:与 ApplicationRunner 类似,但参数为 String[]

适用场景:需要接收简单命令行参数时使用。

示例代码:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner 执行");// 执行逻辑,可访问 args 参数}
}

1.5. 实现 ApplicationListener 接口

作用:通过 ApplicationListener 监听应用完全启动事件。

适用场景:需要在应用完全就绪后执行操作(如启动后发送通知)。

示例代码:

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {System.out.println("ApplicationReadyEvent 触发,应用已启动");// 执行逻辑,此时可安全访问所有Bean}
}

1.6. 使用 @Bean 的 initMethod 属性

作用:在Spring配置类中定义Bean时,通过 initMethod 指定初始化方法。

示例代码:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean(initMethod = "init")public MyBean myBean() {return new MyBean();}
}public class MyBean {public void init() {System.out.println("应用启动后执行: initMethod");// 在这里添加初始化逻辑}
}

2.各方式执行时机对比

方法执行时机
@PostConstructBean 初始化完成后(属性注入后)。
InitializingBean与 @PostConstruct 类似,属性注入后。
ApplicationRunner应用上下文刷新后,ApplicationReadyEvent 触发前。
CommandLineRunner同上。
ApplicationListener应用完全就绪(所有Bean初始化完成,所有启动任务执行完毕)。

根据需求选择合适的方式:

  • 需要访问数据库或资源时,建议使用 ApplicationListener 或 CommandLineRunner
  • 简单的Bean初始化逻辑可用 @PostConstruct 或 InitializingBean

注意事项

  1. 线程问题:默认在主线程执行,避免长时间阻塞操作,可考虑异步执行(如结合 @Async)。
  2. 依赖注入:在 @PostConstruct 和 InitializingBean 中无法直接访问其他Bean(需等待初始化完成)。
  3. 顺序控制:通过 @Order 或实现 Ordered 接口控制多个启动任务的顺序。

3.使用 @Order 控制执行顺序

作用:通过 @Order 或实现 Ordered 接口控制多个启动任务的执行顺序。

示例:在 ApplicationRunner 中使用 @Order

import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
@Order(1)  // 数值越小优先级越高
public class FirstRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {System.out.println("第一个执行的 Runner");}
}@Component
@Order(2)
public class SecondRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {System.out.println("第二个执行的 Runner");}
}
      http://www.dtcms.com/wzjs/496488.html

      相关文章:

    1. 网站案例模版谷歌play
    2. 营销型网站建设专家代发新闻稿最大平台
    3. 用源码怎么做网站谷歌官网注册入口
    4. 哪个新闻网站好郑州seo顾问培训
    5. 网站开发者模式有什么用免费注册网站有哪些
    6. 做简单的html网站网站结构有哪几种
    7. 中国域名网站站内关键词自然排名优化
    8. vs用户登录注册网站建设代码seo信息查询
    9. 亚马逊雨林有原始人吗seo课程培训入门
    10. 新疆省建设厅网站百度指数需求图谱
    11. 网站建设中模全世界足球排名国家
    12. 雄县做网站百度小说风云榜排名
    13. 网站建设公司网站建设专业品牌网站注册查询
    14. 帮人做钓鱼网站以及维护新闻头条 今天
    15. 网站做app开发工具衡水网站优化推广
    16. 在线小公司网站制作长春网站优化服务
    17. 在线电影网站建设论文广州网站优化方式
    18. 移动电商网站开发关键词在线优化
    19. 机房网络组建方案东莞关键词优化推广
    20. 网站扫码怎么做google搜索优化
    21. 商品展示类网站企业网站设计的基本内容包括哪些
    22. 北京专业网站开发公司如何自己做引流推广
    23. 免费做网站通栏广告百度拉新推广平台
    24. 观澜建网站制作网页需要多少钱
    25. 网站开发pc版与手机版制作网页一般多少钱
    26. 做网站开发很赚钱吗抚顺网站建设
    27. 用记事本做网站网络营销包括
    28. 厦门网站seo建设seo搜索优化公司报价
    29. 蓝色云主机一键wordpressseo的中文含义是什么意思
    30. 四川城乡建设委员会官方网站朝阳网站seo