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包
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.8.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.spring</groupId>
- <artifactId>springBoot-demo1</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- </project>
新建文件 IndexController.java
- package com.spring;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @EnableAutoConfiguration
- @RestController
- public class IndexController {
-
-
- // 访问路径 http://localhost:8080/index
- @RequestMapping("/index")
- public String index(){
- System.out.println("我进来了");
- return "index controller";
- }
-
- public static void main(String[] args) {
- // 启动springboot
- SpringApplication.run(IndexController.class,args);
- }
- }
运行main方法,后即可启动项目了
-
-
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v2.0.8.RELEASE)
-
- 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)
- 2021-06-03 20:37:26.014 INFO 942 --- [ main] com.spring.IndexController : No active profile set, falling back to default profiles: default
- 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
- 2021-06-03 20:37:27.329 INFO 942 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
- 2021-06-03 20:37:27.365 INFO 942 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
- 2021-06-03 20:37:27.365 INFO 942 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.37
- 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:.]
- 2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
- 2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1413 ms
- 2021-06-03 20:37:27.520 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
- 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
- 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
- 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
- 2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
- 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]
- 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
- 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()
- 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)
- 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)
- 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]
- 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]
- 2021-06-03 20:37:27.938 INFO 942 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
- 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 ''
- 2021-06-03 20:37:27.990 INFO 942 --- [ main] com.spring.IndexController : Started IndexController in 2.649 seconds (JVM running for 3.063)
- 2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
- 2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
- 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
- package com.spring.controller;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.context.annotation.ComponentScan;
-
- @EnableAutoConfiguration // 开启自动装配
- @ComponentScan("com.spring.controller")
- public class App {
-
- public static void main(String[] args) {
- // 启动springboot
- SpringApplication.run(App.class,args);
- }
- }
-
控制层 OrderController.java
- package com.spring.controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class OrderController {
-
- @RequestMapping("/order")
- public String order(){
- System.out.println("我进来了 order controller 的 index 方法");
- return "order controller";
- }
- }
启动后访问接口 http://localhost:8080/order
控制台打印结果
第三种方式 @SpringBootApplication
@SpringBootApplication 注解作用
标注这是一个springboot的应用,被标注的类是一个主程序, SpringApplication.run(App.class, args);传入的类App.class必须是被@SpringBootApplication标注的类。
@SpringBootApplication是一个组合注解,组合了其他相关的注解,点进去注解后我们可以看到,这个注解集成了以上2种启动方式的注解;在这里的 @ComponentScan() 注解有一堆东西,它的作用是 将主配置类所在包及其下面所有后代包的所有注解扫描;
SpringBootApplication 使用起来更加简单,只需要一个注解即可完成,
- package com.spring.controller;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-
- @SpringBootApplication
- public class App {
-
- public static void main(String[] args) {
- // 启动springboot
- SpringApplication.run(App.class,args);
- }
- }
启动后控制台打印的信息