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*************

猜你喜欢

转载自blog.csdn.net/qq_29897369/article/details/104585296