1、Spring Boot简介
简化Spring应用开发的一个框架
整个Spring技术栈的一个大整合
J2EE开发的一站式解决方案
2、微服务
架构风格
一个用用应该是一组小服务;可以通过http的方式进行互通
单体应用: All in one
每一个功能元素最终都是一个可独立替换和独立升级的元素
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、编写主程序
package com.it.springboot01helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot01HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01HelloworldApplication.class, args);
}
}
4、编写相关的controller service
package com.it.springboot01helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
5、运行主程序应用
6、简化部署
<!-- java应用打包成可执行jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将应用打成jar, maven右边 package java -jar
pom项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
springboot版本仲裁中心
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web
spring-boot-starter场景启动器
spring boot将所有的功能场景都抽取出来,做成一个个starters启动器
需要什么功能就导入什么场景的启动器
3、SpringBoot导包的选择器
@SpringBootApplication EnableAutoConfiguration AutoConfigurationPackage
EnableAutoConfigurationImportSelector 父类 AutoConfigurationImportSelector
将所有的需要导入的组件以全类名的形式返回,这些组件会被添加到容器中
会给容器导入非常多的自动配置类
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
SpringBoot在启动的时候从类路径下 D:\maven_jar\org\springframework\boot\spring-boot-autoconfigure\1.5.9.RELEASE\spring-boot-autoconfigure-1.5.9.RELEASE.jar!\META-INF\spring.factories
源码追踪:
@SpringBootApplication
@EnableAutoConfiguration
@Import({EnableAutoConfigurationImportSelector.class})
extends AutoConfigurationImportSelector
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
META-INF/spring.factories
4、使用idea Spring initializer创建项目
idea支持快速创建项目Spring Initializer