SpringBoot初始化四种方式

平常我们经常会有一些需求是项目启动时候加载一下预置数据,常用有一下四种按照加载先后顺序。

  1. 实现InitializingBean 接口
@Component
public class InitBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.printf("InitializingBean init bean *****");
        System.out.printf("");
    }
}


  1. @PostConstruct
@Component
public class TestRunBean {

    @PostConstruct
    public void testRun(){
        System.out.printf("******************PostConstruct*************");
    }
}
  1. 实现ApplicationRunner
@Component
@Order(0)
public class ApplicationRunnerTest implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.printf("*****************************applicationContext ***********");
        System.out.printf("***");
    }
}

  1. 实现CommandLineRunner
@Component
@Order(3)
public class initTask implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.printf("CommandLineRunner task init**************");
        System.out.printf("");
    }
}
  • 运行如下:
start***********
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2020-02-29 23:55:11.470  INFO 19576 --- [           main] com.example.test.TestApplication         : Starting TestApplication on LAPTOP-7AKLRNUS with PID 19576 (D:\study_home\Test\target\classes started by 17202 in D:\study_home\Test)
2020-02-29 23:55:11.473  INFO 19576 --- [           main] com.example.test.TestApplication         : No active profile set, falling back to default profiles: default
2020-02-29 23:55:11.517  INFO 19576 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@79ca92b9: startup date [Sat Feb 29 23:55:11 CST 2020]; root of context hierarchy
2020-02-29 23:55:12.403  INFO 19576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-02-29 23:55:12.423  INFO 19576 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-02-29 23:55:12.423  INFO 19576 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2020-02-29 23:55:12.428  INFO 19576 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\work_install\jdk1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\17202\AppData\Local\Microsoft\WindowsApps;D:\work_install\jdk1.8\bin;D:\work_install\mysql-5.7.14-winx64\bin;D:\work_install\apache-maven-3.5.4\bin;;.]
2020-02-29 23:55:12.513  INFO 19576 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-02-29 23:55:12.513  INFO 19576 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 999 ms
2020-02-29 23:55:12.632  INFO 19576 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2020-02-29 23:55:12.636  INFO 19576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-02-29 23:55:12.636  INFO 19576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-02-29 23:55:12.637  INFO 19576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-02-29 23:55:12.637  INFO 19576 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
InitializingBean init bean ***********************PostConstruct*************2020-02-29 23:55:12.736  INFO 19576 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-29 23:55:12.921  INFO 19576 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@79ca92b9: startup date [Sat Feb 29 23:55:11 CST 2020]; root of context hierarchy
2020-02-29 23:55:12.975  INFO 19576 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-02-29 23:55:12.976  INFO 19576 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-29 23:55:13.003  INFO 19576 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-29 23:55:13.003  INFO 19576 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-29 23:55:13.132  INFO 19576 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-02-29 23:55:13.169  INFO 19576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-29 23:55:13.172  INFO 19576 --- [           main] com.example.test.TestApplication         : Started TestApplication in 2.006 seconds (JVM running for 3.884)
*****************************applicationContext **************CommandLineRunner task init**************end*************
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛乌龟一起走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值