Spring Boot是Spring開源組織下的子項(xiàng)目,是Spring組件一站式解決方案族壳,主要是簡(jiǎn)化了使用Spring的難度憔辫,簡(jiǎn)省了繁重的配置,提供了各種啟動(dòng)器仿荆,開發(fā)者能快速上手贰您。
SpringBoot優(yōu)點(diǎn)
1、獨(dú)立運(yùn)行
SpringBoot內(nèi)嵌了各種servlet容器拢操,tomcat锦亦,jetty等,現(xiàn)在不再需要打成war包部署到容器中令境,SpringBoot只要達(dá)成一個(gè)可執(zhí)行的jar包就能獨(dú)立運(yùn)行杠园,所有的依賴包都在一個(gè)jar包內(nèi)。
2舔庶、簡(jiǎn)化配置
spring-boot-starter-web啟動(dòng)器自動(dòng)依賴其他組件抛蚁,簡(jiǎn)少了maven的配置。
3惕橙、自動(dòng)配置
Spring Boot能根據(jù)當(dāng)前類路徑下的類瞧甩、jar包來自動(dòng)配置bean,如添加一個(gè)spring-boot-starter-web啟動(dòng)器就能擁有web的功能吕漂,無需其他配置亲配。
4、無代碼生成和XML配置
Spring Boot配置過程中無代碼生成惶凝,也無需XML配置文件就能完成所有配置工作吼虎,這一切都是借助于條件注解完成的,這也是Spring4.x的核心功能之一苍鲜。
5思灰、SpringBoot添加依賴包兩種方式
第一種
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencies>
</dependencyManagement>
第二種
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
SpringBoot的優(yōu)點(diǎn)在于依賴傳遞
但是springboot也有一些缺點(diǎn),在我們項(xiàng)目的pom文件中繼承了父項(xiàng)目spring-boot-starter-parent混滔,這里規(guī)定了一部分坐標(biāo)的版本洒疚、依賴管理歹颓、插件管理都提前定好,所以我們的pom文件繼承spring-boot-starter-parent就直接依賴父項(xiàng)目的所有版本油湖,并且pom文件的便捷性也就體現(xiàn)在版本的傳遞性
SpringBoot依賴覆蓋只對(duì)繼承有效
我們可以在添加的依賴中設(shè)置版本巍扛,但是只對(duì)上文繼承依賴有效,對(duì)第二種導(dǎo)入依賴方式無效
<properties>
<slf4j.version>1.7.25<slf4j.version>
</properties>
如果希望實(shí)現(xiàn)導(dǎo)入方式的版本升級(jí)需要把要升級(jí)的組件放到導(dǎo)入的springboot父依賴之上
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
需要注意乏德,要修改Spring Boot的依賴組件版本可能會(huì)造成不兼容的問題撤奸。
SpringBoot的自動(dòng)配置原理
從啟動(dòng)類的@SpringBootApplication
可以找到@EnableAutoConfiguration
所以可以看到Spring Boot的自動(dòng)配置注解就是@EnableAutoConfiguration
@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}
)}
)
我們找到@EnableAutoConfiguration中的@Import({AutoConfigurationImportSelector.class})
,從該類的引用包@import
中可以找到import org.springframework.core.io.support.SpringFactoriesLoader;
喊括,在這個(gè)類的
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
方法可以加載springboot中的配置文件胧瓜,這個(gè)方法會(huì)調(diào)用本類中
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
if (result != null) {
return result;
} else {
try {
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
LinkedMultiValueMap result = new LinkedMultiValueMap();
while(urls.hasMoreElements()) {
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Iterator var6 = properties.entrySet().iterator();
while(var6.hasNext()) {
Entry<?, ?> entry = (Entry)var6.next();
String factoryClassName = ((String)entry.getKey()).trim();
String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
int var10 = var9.length;
for(int var11 = 0; var11 < var10; ++var11) {
String factoryName = var9[var11];
result.add(factoryClassName, factoryName.trim());
}
}
}
cache.put(classLoader, result);
return result;
} catch (IOException var13) {
throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
}
}
}
這個(gè)方法會(huì)加載類路徑及所有jar包下META-INF/spring.factories配置中映射的自動(dòng)配置的類public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
這個(gè)常量可以去加載Sring自動(dòng)配置的所有類
我們繼續(xù)查看項(xiàng)目的maven依賴,發(fā)現(xiàn)
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
……
在這一塊配置文件中所有配置自動(dòng)加載的類都會(huì)被加載到springboot中郑什。比如我們看一下關(guān)于Redis的自動(dòng)配置類
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
類被@EnableConfigurationProperties注解府喳,所以SpringBoot會(huì)觸發(fā)對(duì)RedisProperties執(zhí)行自動(dòng)綁定屬性值。
此類會(huì)自動(dòng)創(chuàng)建bean對(duì)象: redis連接池JedisConnectionFactory和redis模板類(RedisTemplate和StringRedisTemplate)蘑拯。直接在應(yīng)用中通過@Autowired就可以注入以上對(duì)象钝满。
繼續(xù)查看RedisProperties.class
package org.springframework.boot.autoconfigure.data.redis;
import java.time.Duration;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(
prefix = "spring.redis"
)
public class RedisProperties {
private int database = 0;
private String url;
private String host = "localhost";
private String password;
private int port = 6379;
private boolean ssl;
private Duration timeout;
private RedisProperties.Sentinel sentinel;
private RedisProperties.Cluster cluster;
private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
}
RedisProperties類被@ConfigurationProperties注解,表示從外部文件(如application.properties)注入屬性值强胰。application.properties中的參數(shù)會(huì)被自動(dòng)封裝到RedisProperties中舱沧,然后加載到自動(dòng)配置類的RedisTemplate的初始化對(duì)象中我們就可以直接注入。
因此我們得到Redis和SpringBoot整合后可以加載的整個(gè)流程:
1偶洋、啟動(dòng)類開始執(zhí)行run方法
2熟吏、啟動(dòng)類main方法被@SpringBootApplication
標(biāo)注,@SpringBootApplication
涵蓋@EnableAutoConfiguration
玄窝,@EnableAutoConfiguration
引入自動(dòng)導(dǎo)入選擇器AutoConfigurationImportSelector.class
牵寺,這個(gè)類調(diào)用SpringFactoriesLoader.loadFactoryNames()
方法加載maven依賴中自動(dòng)導(dǎo)入包下META-INF下的spring.factories
文件,開始自動(dòng)配置這里面的組件恩脂,以RedisAutoConfiguration
為例包含
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
三個(gè)核心注解帽氓,其中@EnableConfigurationProperties
可以自動(dòng)綁定配置文件,并且RedisProperties.class又被RedisProperties類被@ConfigurationProperties
注解俩块,表示從外部文件(如application.properties)注入屬性值黎休。application.properties中的參數(shù)會(huì)被自動(dòng)封裝到RedisProperties中。