`
rensanning
  • 浏览: 3552537 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:38217
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:607585
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:682772
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:89526
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:402123
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69759
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:91834
社区版块
存档分类
最新评论

Spring Boot 入门 - 进阶篇(6)- 启动加载(CommandLineRunner)

 
阅读更多
启动成功后可以通过以下方法运行自己的初始代码:
  • @PostConstruct注解
  • ApplicationReadyEvent事件
  • CommandLineRunner/ApplicationRunner接口

@Component
public class StartUpInit {

  @Autowired
  private SomeService service;

  @PostConstruct
  public void init(){
     // ...
  }

}


@Component
public class GeneralEventHandler {

    @EventListener
    public void handleApplicationReady(ApplicationReadyEvent event) {
        log.info("The application is ready to service requests..");
    }

}


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

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

(1)CommandLineRunner:参数是字符串数组
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
  private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);

  @Override
  public void run(String... args) throws Exception {
    logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
  }
}


(2)ApplicationRunner:参数被放入ApplicationArguments
通过getOptionNames()、getOptionValues()、getSourceArgs()获取参数
@Component
public class AppStartupRunner implements ApplicationRunner {
  private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class);

  @Override
  public void run(ApplicationArguments args) throws Exception {
    logger.info("Your application started with option names : {}", args.getOptionNames());
  }
}


也可以两个接口同时实现,但是没有必要。
@Component
public class StartupRunner implements CommandLineRunner, ApplicationRunner  {
  private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);

  @Override
  public void run(String... args) throws Exception {
    logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
  }
  
  @Override
  public void run(ApplicationArguments args) throws Exception {
    logger.info("Your application started with option names : {}", args.getOptionNames());
  }
}


也可以通过@Bean定义
@Configuration
public class RunnerConfig {

	@Bean
	public CommandLineRunner runner(){
	  return new CommandLineRunner() {
	    public void run(String... args){
	      System.out.println("CommandLineRunner run()");
	    }
	  };
	}

}


(3)通过@Order设置执行顺序
@Component
@Order(3)
public class Runner1 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("Runner1 run()");
  }
}

@Component
@Order(2)
public class Runner2 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("Runner2 run()");
  }
}

@Component
@Order(1)
public class Runner3 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("Runner3 run()");
  }
}


(4)注入Bean
CommandLineRunner在被执行时,Spring内部已经启动完成,可以注入Spring的Bean。
@Component
public class StartupRunner implements CommandLineRunner {

  @Autowired
  private SampleService sampleService;

  @Override
  public void run(String... args) throws Exception {
    sampleService.executeSample();
  }
  
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics