SpringBoot---概述
Overview
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
Spring Boot 让创建 独立、生产级别、基于Spring 应用 变得简单;
Features
1、Create stand-alone Spring applications
创建独立的Spring应用;
2、Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
内嵌Tomcat、Jetty(无需部署war包)
3、Provide opinionated 'starter' dependencies to simplify your build configuration
提供 starter,以简化构建应用(依赖管理);
4、Automatically configure Spring and 3rd party libraries whenever possible
尽可能 自动配置Spring与第三方库;
5、Provide production-ready features such as metrics, health checks, and externalized configuration
提供 生产级别的特色:健康检测、远程配置...
6、Absolutely no code generation and no requirement for XML configuration
没有额外的代码生成、无需xml配置;
微服务

依赖管理
父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
父依赖内容

父依赖的父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

将所有常用的依赖统一进行管理
自定义依赖管理
1、查看spring-boot-dependencies的当前规定的依赖版本 用的key
2、在当前pom中重写<properties>
starter
Starters are a set of convenient dependency descriptors that you can include in your application.
Starters是一组 便利的依赖,可以在应用中使用;
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
Starters包含大量 应用需要的依赖;
官方starter
All official starters follow a similar naming pattern; spring-boot-starter-*, where * is a particular type of application.
所有官方的starters命名规则:spring-boot-starter-*
自定义starter
As explained in the “Creating Your Own Starter” section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts.
第三方自定义starter,不允许以spring-boot开头;
Rather, a third-party starter typically starts with the name of the project.For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.
第三方starter可以用项目名称开头;比如 *-spring-boot-starter;
注意
所有的starter最终都会依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
自动配置
各种配置拥有默认值
默认配置最终都映射到MultipartProperties;
配置文件的值,最终都会绑定到某个类,这个类会在容器中创建对象;
按需加载所有自动配置
引入哪个才会开启哪个的自动配置;
SpringBoot的所有的自动配置功能都在spring-boot-autoconfigure包中;
容器功能
组件添加
xml
<bean></bean>
@Component、@Controller、@Service、@Repository
@Configuration
/**
* 1、配置类使用@Bean添加Bean,默认是单例;
* 2、配置类本身也是Bean;
* 3、@Configuration的proxyBeanMethods:
* 3.1、Full:
* proxyBeanMethods = true;
* 保证每个@Bean的方法被调用N次,返回的Bean都是同一个单例Bean;
* 3.2、Lite:
* proxyBeanMethods = false;
* 每次调用@Bean的方法,都会创建一个新的Bean;
*
* 【注意】
* Bean之间相互依赖,必须使用Full
*/
@Configuration(proxyBeanMethods = true) // Config.java 等价于 xx.xml
public class Config {
@Bean // 给容器添加Bean (默认方法名为Bean的id、返回值类型即Bean类型)
public User user(){
return new User();
}
}
@CompomentScan
指定Bean扫描路径
@Import
给容器中创建指定类型的Bean;
默认的Bean名称:全类名;
@Conditional
条件添加Bean;
在类上,满足条件,类中的内容才生效;
在方法上,满足条件,方法才生效;

导入配置文件
@ImportResource
加载指定资源文件
配置类使用;
@ImportResource(value = "bean.xml")
配置绑定
如何使用Java读取properties文件中的内容,并将其转换为JavaBean;
@Component+@ConfigurationProperties
/**
* 只有容器中的Bean,才能拥有SpringBoot的强大功能
*/
@Component
@ConfigurationProperties(prefix = "man")
public class Man {
private String name;
private int age;
get,set...
}
applcation.xml
... man.name=rose man.age=13 ...
@EnableConfigurationProperties+@ConfigurationProperties
@ConfigurationProperties(prefix = "man")
public class Man {
private String name;
private int age;
get,set...
}
Config.java
@EnableConfigurationProperties(value = Man.class) // 1、开启指定Bean的配置绑定 2、将指定Bean注入容器中
public class Config {
...
}
applcation.xml
man.name=rose man.age=13
浙公网安备 33010602011771号