1、Sping Boot简介
简化Spring应用开发的一个框架
整个Spring技术栈的一个大整合
J2EE 开发的一站式解决方案
人们把Spring Boot 称为搭建程序的脚手架,它推崇约定大于配置的方式使得开发人员能够快速的构建生产级别的spring应用。并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让我们关注于业务而非配置
优点:
1、快速创建独立运行的Spring项目以及与主流框架集成
2、使用嵌入式的Servlet容器,应用无需打成war包
3、starters自动依赖与版本控制
4、大量的自动配置,简化开发
5、无需配置xml,无代码生成,开箱即用
6、准生成环境 运行时应用监控
2、微服务
微服务:是一种架构风格
一个应用应该是一组小型服务,可以通过HTTP方式进行互通
每一个功能元素最终都是一个可独立替换和独立升级的软件单元
3、环境准备
环境约束
–jdk1.8:Spring Boot 推荐jdk1.7及以上;java version "1.8.0_112"
–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、STS
–SpringBoot 1.5.9.RELEASE:1.5.9;
4、Spring Boot HelloWorld
1、创建一个maven工程
2、导入spring boot的相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入配置文件处理器 配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--spring boot 的单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 这个插件,可以将应用达成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、编写主程序
作用:启动Spring Boot项目
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
// 启动spring 应用
SpringApplication.run(HelloWorldApplication.class,args);
}
}
4、编写 业务逻辑
@Controller
public class HelloWorldController {
@ResponseBody
@GetMapping("/hello")
public String hello(){
return "hello world,my first spring boot application!";
}
}
5、运行主程序测试
6、简化部署
<!-- 这个插件,可以将应用达成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将这个应用达成jar包,直接使用java -jar的命令执行
(why==》jar包内包含了嵌入式的tomcat)
5、HelloWorld 一探究竟
1、pom文件
1)父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
他的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
这称为 Spring Boot的版本仲裁中心
==》 以后我们导入依赖时不需要写版本的
没有在dependencies管理的依赖自然要写jar包
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 将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入
这些starter,那么相关场景的所有依赖都会导入进来。要用什么功能,就导入什么场景的启动器
2、主程序类(主入口类)
@SpringBootApplication
public 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 {
@SpringBootConfiguration :标志在某个类上,标志着这是一个 SpringBoot的配置类
@Configuration :配置类上标识的注解;
配置类 === 配置文件 ;配置类也是容器中的一个组件 @Component
@EnableAutoConfiguration: 开启自动配置功能
以前我们需要配置的东西,Spring Boot会帮助我们自动配置;@EnableAutoConfiguration
告诉SpringBoot开启自动配置功能,这样自动配置才能生效
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class})
Spring的底层注解@import,给容器中导入一个组件:导入的组件由Registrar.class 指定
@AutoConfigurationPackage作用:
将主配置类(@SpringBootApplication标注的类)所在包及下面的所有子包里面的所有组件扫描
到Spring容器
@Import({AutoConfigurationImportSelector.class})