一、Spring Boot 简介
J2EE的开发略显轻便,配置繁多、 部署流程简单、第三方技术集成难度大,都很大水平上升高了开发效率。SpringBoot应运而生,SpringBoot简化了Spring利用开发,整合了Spring技术栈, 去繁从简,just run就能创立一个独立的、产品级别的利用,为J2EE开发提供了一站式解决方案。
- 疾速创立独立运行的Spring我的项目以及与支流框架集成
- 应用嵌入式的Servlet容器,利用无需打成WAR包
- starters主动依赖与版本控制
- 大量的主动配置,简化开发,也可批改默认值
- 无需配置XML,无代码生成,开箱即用
- 准生产环境的运行时利用监控
- 与云计算的人造集成
二、入门案例:HelloWorld
一个性能:浏览器发送hello申请,服务器承受申请并解决,响应Hello World字符串。
2.1 创立maven工程
2.2 引入starter
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.3 编写主程序
/** * @SpringBootApplication 标注主程序类,阐明这是一个Spring Boot利用 */@SpringBootApplicationpublic class HelloWorldApplication { public static void main(String[] args) { // 启动Spring利用 SpringApplication.run(HelloWorldApplication.class,args); }}
2.4 编写Controller
@Controllerpublic class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello World!"; }}
2.5 运行主程序
关上本地Terminal:
C:\WorkSpace\IdeaProjects\springboot-learn>curl http://localhost:8080/helloHello World!
关上Google,拜访http://localhost:8080/hello
2.6 简化部署
<!-- 这个插件,能够将利用打包成一个可执行的jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
将这个利用打成jar包,间接应用java -jar
的命令进行执行。
C:\WorkSpace\IdeaProjects\springboot-learn>cd springboot-helloworld (切换到pom.xml所在目录)C:\WorkSpace\IdeaProjects\springboot-learn\springboot-helloworld>mvn clean package -Dmaven.test.skip[INFO] Scanning for projects...[INFO][INFO] -----------------------< com.kai:springboot-helloworld >-----------------------[INFO] Building springboot-helloworld 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]--------------------------------- ---------------------------------snip-----------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 11.092 s[INFO] Finished at: 2020-10-16T10:37:55+08:00[INFO] ------------------------------------------------------------------------C:\WorkSpace\IdeaProjects\springboot-learn\springboot-helloworld>cd targetC:\WorkSpace\IdeaProjects\springboot-learn\springboot-helloworld\target>dir 驱动器 D 中的卷是 Data 卷的序列号是 B8A9-CEA2 C:\WorkSpace\IdeaProjects\springboot-learn\springboot-helloworld\target 目录2020/10/16 10:37 <DIR> .2020/10/16 10:37 <DIR> ..2020/10/16 10:37 <DIR> classes2020/10/16 10:37 <DIR> generated-sources2020/10/16 10:37 <DIR> maven-archiver2020/10/16 10:37 <DIR> maven-status2020/10/16 10:37 16,519,536 springboot-helloworld-1.0-SNAPSHOT.jar2020/10/16 10:37 3,081 springboot-helloworld-1.0-SNAPSHOT.jar.original 2 个文件 16,522,617 字节 6 个目录 34,072,743,936 可用字节C:\WorkSpace\IdeaProjects\springboot-learn\springboot-helloworld\target>java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar
关上Google,拜访http://localhost:8080/hello
三、Hello World案例剖析
3.1 POM文件
(1)父我的项目
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version></parent>它的父我的项目是:真正治理Spring Boot利用外面的所有依赖版本<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.3.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath></parent>
Spring Boot的版本仲裁核心,所以再在dependencies外面治理的依赖不须要写版本。
(2)场景启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
spring-boot-starter-==web==
spring-boot-starter:spring-boot场景启动器,帮咱们导入了web模块失常运行所依赖的组件。
Spring Boot将所有的性能场景都抽取进去,做成一个个的starter(启动器),只须要在我的项目外面引入这些starter相干场景的所有依赖都会导入进来,要用什么性能就导入什么场景的启动器。
3.2 主程序类
/** * @SpringBootApplication 标注主程序类,阐明这是一个Spring Boot利用 */@SpringBootApplicationpublic class HelloWorldApplication { public static void main(String[] args) { // 启动Spring利用 SpringApplication.run(HelloWorldApplication.class,args); }}
@SpringBootApplication 标注在某个类上阐明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main办法来启动SpringBoot利用。
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})})public @interface SpringBootApplication {
(1) @SpringBootConfiguration
@SpringBootConfiguration标注在某个类上,示意这是一个Spring Boot的主配置类,SpringBoot启动这个类的main办法启动利用。
@Configurationpublic @interface SpringBootConfiguration {
- @Configuration:用于指定以后类是一个 Spring 配置类,当创立容器时会从该类上加载注解。
@Componentpublic @interface Configuration {
- @Component 标注一个类为Spring容器的Bean,(把一般pojo实例化到Spring容器中,相当于配置文件中的
<bean id="" class=""/>
)
(2) @EnableAutoConfiguration
开启主动配置性能
@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:主动配置包
@Import({Registrar.class})public @interface AutoConfigurationPackage {
- @Import({Registrar.class})
- Spring的底层注解@Import,给容器中导入一个组件,导入的组件由Registrar.class指定,==将主配置类(@SpringBootApplication标注的类)的所在包及上面所在子包外面的所有组件扫描到Spring容器。==
@Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector:导入组件的选择器,将所有须要导入的组件以全类名的形式返回,这些组件就会被增加到容器中。会给容器中导入十分多的主动配置类(xxxAutoConfiguration),就是给容器中导入这个场景须要的所有组件,并配置好这些组件。
查看源码:
查看spring.factories:
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\\--- snip ---\\--- snip ---\\--- snip ---\org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration
可见,==Spring Boot在启动的时候从类门路下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为主动配置类导入到容器中,主动配置类就失效,帮咱们进行主动配置工作==。
J2EE的整体整合解决方案和主动配置都在spring-boot-autoconfigure-2.3.3.RELEASE.jar
中。