spring boot 项目启动初始化资源或配置的方式

转载来自:https://rensanning.iteye.com/blog/2363313

 

启动成功后可以通过以下方法运行自己的初始代码: 

  • @PostConstruct注解
  • ApplicationReadyEvent事件
  • CommandLineRunner/ApplicationRunner接口


Java代码 

 收藏代码

  1. @Component  
  2. public class StartUpInit {  
  3.   
  4.   @Autowired  
  5.   private SomeService service;  
  6.   
  7.   @PostConstruct  
  8.   public void init(){  
  9.      // ...  
  10.   }  
  11.   
  12. }  



Java代码 

 收藏代码

  1. @Component  
  2. public class GeneralEventHandler {  
  3.   
  4.     @EventListener  
  5.     public void handleApplicationReady(ApplicationReadyEvent event) {  
  6.         log.info("The application is ready to service requests..");  
  7.     }  
  8.   
  9. }  



Spring Boot提供了两个接口:CommandLineRunner、ApplicationRunner,用于启动应用时做特殊处理,这些代码会在SpringApplication的run()方法运行完成之前被执行。 
通常用于应用启动前的特殊代码执行、特殊数据加载、垃圾数据清理、微服务的服务发现注册、系统启动成功后的通知等。相当于Spring的ApplicationListener、Servlet的ServletContextListener。 

CommandLineRunner 和 ApplicationRunner 的区别是run()方法的参数不同。 

(1)CommandLineRunner:参数是字符串数组 
Java代码 

 收藏代码

  1. @Component  
  2. public class CommandLineAppStartupRunner implements CommandLineRunner {  
  3.   private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);  
  4.   
  5.   @Override  
  6.   public void run(String... args) throws Exception {  
  7.     logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));  
  8.   }  
  9. }  



(2)ApplicationRunner:参数被放入ApplicationArguments 
通过getOptionNames()、getOptionValues()、getSourceArgs()获取参数 
Java代码 

 收藏代码

  1. @Component  
  2. public class AppStartupRunner implements ApplicationRunner {  
  3.   private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class);  
  4.   
  5.   @Override  
  6.   public void run(ApplicationArguments args) throws Exception {  
  7.     logger.info("Your application started with option names : {}", args.getOptionNames());  
  8.   }  
  9. }  



也可以两个接口同时实现,但是没有必要。 
Java代码 

 收藏代码

  1. @Component  
  2. public class StartupRunner implements CommandLineRunner, ApplicationRunner  {  
  3.   private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);  
  4.   
  5.   @Override  
  6.   public void run(String... args) throws Exception {  
  7.     logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));  
  8.   }  
  9.     
  10.   @Override  
  11.   public void run(ApplicationArguments args) throws Exception {  
  12.     logger.info("Your application started with option names : {}", args.getOptionNames());  
  13.   }  
  14. }  



也可以通过@Bean定义 
Java代码 

 收藏代码

  1. @Configuration  
  2. public class RunnerConfig {  
  3.   
  4.     @Bean  
  5.     public CommandLineRunner runner(){  
  6.       return new CommandLineRunner() {  
  7.         public void run(String... args){  
  8.           System.out.println("CommandLineRunner run()");  
  9.         }  
  10.       };  
  11.     }  
  12.   
  13. }  



(3)通过@Order设置执行顺序 
Java代码 

 收藏代码

  1. @Component  
  2. @Order(3)  
  3. public class Runner1 implements CommandLineRunner {  
  4.   @Override  
  5.   public void run(String... args) throws Exception {  
  6.     System.out.println("Runner1 run()");  
  7.   }  
  8. }  
  9.   
  10. @Component  
  11. @Order(2)  
  12. public class Runner2 implements CommandLineRunner {  
  13.   @Override  
  14.   public void run(String... args) throws Exception {  
  15.     System.out.println("Runner2 run()");  
  16.   }  
  17. }  
  18.   
  19. @Component  
  20. @Order(1)  
  21. public class Runner3 implements CommandLineRunner {  
  22.   @Override  
  23.   public void run(String... args) throws Exception {  
  24.     System.out.println("Runner3 run()");  
  25.   }  
  26. }  



(4)注入Bean 
CommandLineRunner在被执行时,Spring内部已经启动完成,可以注入Spring的Bean。 
Java代码 

 收藏代码

  1. @Component  
  2. public class StartupRunner implements CommandLineRunner {  
  3.   
  4.   @Autowired  
  5.   private SampleService sampleService;  
  6.   
  7.   @Override  
  8.   public void run(String... args) throws Exception {  
  9.     sampleService.executeSample();  
  10.   }  
  11.     
  12. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值