SpringBoot的三种启动方式

发布时间:2022-03-01 11:21:23 作者:yexindonglai@163.com 阅读(862)

springBoot 是什么

    springboot 是一个框架,它依赖于spring,在过去我们使用spring项目的时候,需要大量各种繁琐的配置文件才能让spring跑起来,但自从有了springboot之后,它简化了使用方式,做到了无xml文件的配置即可运行,最大的特点是让配置文件变得简单,得益于自身的自动装配功能,部署也变得更简单(springboot内置web容器服务器tomcat),加上热部署后,可以快速开发和运行一个项目;

    spring大家都知道,boot是启动的意思。所以,spring boot其实就是一个启动spring项目的一个工具而已,总而言之,springboot 是一个服务于框架的框架;

 

Spring Boot的核心功能

1、 可独立运行的Spring项目:Spring Boot可以以jar包的形式独立运行。


2、 内嵌的Servlet容器:Spring Boot可以选择内嵌Tomcat、Jetty或者Undertow,无须以war包形式部署项目。

3、 简化的Maven配置:Spring提供推荐的基础 POM 文件来简化Maven 配置。

4、 自动配置Spring:Spring Boot会根据项目依赖来自动配置Spring 框架,极大地减少项目要使用的配置。

5、 提供生产就绪型功能:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。

6、 无代码生成和xml配置:Spring Boot不生成代码。完全不需要任何xml配置即可实现Spring的所有配置。

 

第一种启动方式  @EnableAutoConfiguration

@EnableAutoConfiguration 作用

    开启自动装配,帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot,并创建对应配置类的Bean,并把该Bean实体交给IoC容器进行管理。

在使用springboot之前,我们需要先导入相关的jar包

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.0.8.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.spring</groupId>
  13. <artifactId>springBoot-demo1</artifactId>
  14. <version>1.0-SNAPSHOT</version>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. </dependencies>
  30. </project>

新建文件  IndexController.java

  1. package com.spring;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @EnableAutoConfiguration
  7. @RestController
  8. public class IndexController {
  9. // 访问路径 http://localhost:8080/index
  10. @RequestMapping("/index")
  11. public String index(){
  12. System.out.println("我进来了");
  13. return "index controller";
  14. }
  15. public static void main(String[] args) {
  16. // 启动springboot
  17. SpringApplication.run(IndexController.class,args);
  18. }
  19. }

运行main方法,后即可启动项目了

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.0.8.RELEASE)
  8. 2021-06-03 20:37:26.010 INFO 942 --- [ main] com.spring.IndexController : Starting IndexController on xiexindongdeMacBook-Pro.local with PID 942 (/Users/yexindong/Documents/java/java_project/springBootdemo1/target/classes started by yexindong in /Users/yexindong/Documents/java/java_project)
  9. 2021-06-03 20:37:26.014 INFO 942 --- [ main] com.spring.IndexController : No active profile set, falling back to default profiles: default
  10. 2021-06-03 20:37:26.064 INFO 942 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@105fece7: startup date [Thu Jun 03 20:37:26 CST 2021]; root of context hierarchy
  11. 2021-06-03 20:37:27.329 INFO 942 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  12. 2021-06-03 20:37:27.365 INFO 942 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2021-06-03 20:37:27.365 INFO 942 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.37
  14. 2021-06-03 20:37:27.375 INFO 942 --- [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: [/Users/yexindong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
  15. 2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  16. 2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1413 ms
  17. 2021-06-03 20:37:27.520 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
  18. 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  19. 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  20. 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
  21. 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  22. 2021-06-03 20:37:27.619 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  23. 2021-06-03 20:37:27.750 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@105fece7: startup date [Thu Jun 03 20:37:26 CST 2021]; root of context hierarchy
  24. 2021-06-03 20:37:27.805 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.spring.IndexController.index()
  25. 2021-06-03 20:37:27.808 INFO 942 --- [ 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)
  26. 2021-06-03 20:37:27.808 INFO 942 --- [ 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)
  27. 2021-06-03 20:37:27.825 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  28. 2021-06-03 20:37:27.825 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  29. 2021-06-03 20:37:27.938 INFO 942 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  30. 2021-06-03 20:37:27.986 INFO 942 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  31. 2021-06-03 20:37:27.990 INFO 942 --- [ main] com.spring.IndexController : Started IndexController in 2.649 seconds (JVM running for 3.063)
  32. 2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
  33. 2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
  34. 2021-06-03 20:37:40.491 INFO 942 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms

在浏览器输入网址访问 controller :http://localhost:8080/index  页面结果如下:

控制台也输出了打印的内容,是不是很简单呢?

 

第二种启动方式 @ComponentScan

@ComponentScan() 注解作用

     根据定义的扫描路径,将符合规则的类加载到spring容器中,比如在类中加入了以下注解 @Controller、@Service、@Mapper 、@Component、@Configuration 等等;

接下来我们测试一下,创建启动类  App.java

  1. package com.spring.controller;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.context.annotation.ComponentScan;
  5. @EnableAutoConfiguration // 开启自动装配
  6. @ComponentScan("com.spring.controller")
  7. public class App {
  8. public static void main(String[] args) {
  9. // 启动springboot
  10. SpringApplication.run(App.class,args);
  11. }
  12. }

控制层 OrderController.java 

  1. package com.spring.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class OrderController {
  6. @RequestMapping("/order")
  7. public String order(){
  8. System.out.println("我进来了 order controller 的 index 方法");
  9. return "order controller";
  10. }
  11. }

启动后访问接口  http://localhost:8080/order  

控制台打印结果

 

第三种方式  @SpringBootApplication

@SpringBootApplication 注解作用

标注这是一个springboot的应用,被标注的类是一个主程序, SpringApplication.run(App.class, args);传入的类App.class必须是被@SpringBootApplication标注的类。

@SpringBootApplication是一个组合注解,组合了其他相关的注解,点进去注解后我们可以看到,这个注解集成了以上2种启动方式的注解;在这里的 @ComponentScan() 注解有一堆东西,它的作用是 将主配置类所在包及其下面所有后代包的所有注解扫描;

 SpringBootApplication 使用起来更加简单,只需要一个注解即可完成,

  1. package com.spring.controller;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class App {
  6. public static void main(String[] args) {
  7. // 启动springboot
  8. SpringApplication.run(App.class,args);
  9. }
  10. }

启动后控制台打印的信息

关键字SpringBoot