用Srping Boot 写一个 Hello world

用Srping Boot 写一个 Hello world

一般我们在学习一门新的语言时,这里我没有换语言,还是使用java,只是不同于普通的使用main方法的java程序,而是使用SpringBoot来写一个最简单的“Hello world”程序。

TODO 相关使用场景

通过实现ApplicationRunner接口来实现程序启动时执行

ApplicationRunner

  1. package com.github.niushuai1991.jarapp;
  2. import org.springframework.boot.CommandLineRunner;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class MyCommandLineRunner implements CommandLineRunner {
  6. @Override
  7. public void run(String... args) {
  8. System.out.println("Hello world! CommandLineRunner");
  9. }
  10. }

通过实现CommandLineRunner接口来实现程序启动时执行

  1. package com.github.niushuai1991.jarapp;
  2. import org.springframework.boot.ApplicationArguments;
  3. import org.springframework.boot.ApplicationRunner;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class MyApplicationRunner implements ApplicationRunner {
  7. @Override
  8. public void run(ApplicationArguments args) {
  9. System.out.println("Hello world! ApplicationRunner");
  10. }
  11. }

当程序启动后,我们在控制台里就能看到以输出内容:

  1. Hello world! ApplicationRunner
  2. Hello world! CommandLineRunner

猜你喜欢

转载自www.cnblogs.com/niushuaiblog/p/12612842.html