Springboot的優點
- 内置servlet容器,不需要在伺服器部署 tomcat。隻需要将項目打成 jar 包,使用 java -jar xxx.jar一鍵式啟動項目
- SpringBoot提供了starter,把常用庫聚合在一起,簡化複雜的環境配置,快速搭建spring應用環境
- 可以快速建立獨立運作的spring項目,內建主流架構
- 準生産環境的運作應用監控
SpringBoot 中的 starter 到底是什麼 ?
starter提供了一個自動化配置類,一般命名為 XXXAutoConfiguration ,在這個配置類中通過條件注解來決定一個配置是否生效(條件注解就是 Spring 中原本就有的),然後它還會提供一系列的預設配置,也允許開發者根據實際情況自定義相關配置,然後通過類型安全的屬性注入将這些配置屬性注入進來,新注入的屬性會代替掉預設屬性。正因為如此,很多第三方架構,我們隻需要引入依賴就可以直接使用了。
運作 SpringBoot 有哪幾種方式?
- 打包用指令或者者放到容器中運作
- 用 Maven/Gradle 插件運作
- 直接執行 main 方法運作
SpringBoot 常用的 Starter 有哪些?
- spring-boot-starter-web :提供 Spring MVC + 内嵌的 Tomcat 。
- spring-boot-starter-data-jpa :提供 Spring JPA + Hibernate 。
- spring-boot-starter-data-Redis :提供 Redis 。
- mybatis-spring-boot-starter :提供 MyBatis 。
Spring Boot 的核心注解是哪個?
啟動類上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要組合包含了以下 3 個注解:
- @SpringBootConfiguration:組合了 @Configuration 注解,實作配置檔案的功能。
- @EnableAutoConfiguration:打開自動配置的功能,也可以關閉某個自動配置的選項,如關閉資料源自動配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。
- @ComponentScan:Spring元件掃描。
自動配置原理
SpringBoot實作自動配置原理圖解:
在 application.properties 中設定屬性 debug=true,可以在控制台檢視已啟用和未啟用的自動配置。
@SpringBootApplication是@Configuration、@EnableAutoConfiguration和@ComponentScan的組合。
@Configuration表示該類是Java配置類。
@ComponentScan開啟自動掃描符合條件的bean(添加了@Controller、@Service等注解)。
@EnableAutoConfiguration會根據類路徑中的jar依賴為項目進行自動配置,比如添加了spring-boot-starter-web依賴,會自動添加Tomcat和Spring MVC的依賴,然後Spring Boot會對Tomcat和Spring MVC進行自動配置(spring.factories EnableAutoConfiguration配置了WebMvcAutoConfiguration)。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}
EnableAutoConfiguration主要由 @AutoConfigurationPackage,@Import(EnableAutoConfigurationImportSelector.class)這兩個注解組成的。
@AutoConfigurationPackage用于将啟動類所在的包裡面的所有元件注冊到spring容器。
@Import 将EnableAutoConfigurationImportSelector注入到spring容器中,EnableAutoConfigurationImportSelector通過SpringFactoriesLoader從類路徑下去讀取META-INF/spring.factories檔案資訊,此檔案中有一個key為org.springframework.boot.autoconfigure.EnableAutoConfiguration,定義了一組需要自動配置的bean。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
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,\
這些配置類不是都會被加載,會根據xxxAutoConfiguration上的@ConditionalOnClass等條件判斷是否加載,符合條件才會将相應的元件被加載到spring容器。(比如mybatis-spring-boot-starter,會自動配置sqlSessionFactory、sqlSessionTemplate、dataSource等mybatis所需的元件)
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
AnnotatedElement.class }) //類路徑存在EnableAspectJAutoProxy等類檔案,才會加載此配置類
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
}
全局配置檔案中的屬性如何生效,比如:server.port=8081,是如何生效的?
@ConfigurationProperties的作用就是将配置檔案的屬性綁定到對應的bean上。全局配置的屬性如:server.port等,通過@ConfigurationProperties注解,綁定到對應的XxxxProperties bean,通過這個 bean 擷取相應的屬性(serverProperties.getPort())。
//server.port = 8080
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
private Integer port;
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
private Boolean useForwardHeaders;
private String serverHeader;
//...
}
實作自動配置
實作當某個類存在時,自動配置這個類的bean,并且可以在application.properties中配置bean的屬性。
(1)建立Maven項目spring-boot-starter-hello,修改pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tyson</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.3.0.M1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
(2)屬性配置
public class HelloService {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String sayHello() {
return "hello" + msg;
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="hello")
public class HelloServiceProperties {
private static final String MSG = "world";
private String msg = MSG;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
(3)自動配置類
import com.tyson.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class) //1
@ConditionalOnClass(HelloService.class) //2
@ConditionalOnProperty(prefix="hello", value = "enabled", matchIfMissing = true) //3
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties;
@Bean
@ConditionalOnMissingBean(HelloService.class) //4
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg());
return helloService;
}
}
- @EnableConfigurationProperties 注解開啟屬性注入,将帶有@ConfigurationProperties 注解的類注入為Spring 容器的 Bean。
- 當 HelloService 在類路徑的條件下。
- 當設定 hello=enabled 的情況下,如果沒有設定則預設為 true,即條件符合。
- 當容器沒有這個 Bean 的時候。
(4)注冊配置
想要自動配置生效,需要注冊自動配置類。在 src/main/resources 下建立 META-INF/spring.factories。添加以下内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tyson.config.HelloServiceAutoConfiguration
"\"是為了換行後仍然能讀到屬性。若有多個自動配置,則用逗号隔開。
(5)使用starter
在 Spring Boot 項目的 pom.xml 中添加:
<dependency>
<groupId>com.tyson</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
運作類如下:
import com.tyson.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootDemoApplication {
@Autowired
public HelloService helloService;
@RequestMapping("/")
public String index() {
return helloService.getMsg();
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
在項目中沒有配置 HelloService bean,但是我們可以注入這個bean,這是通過自動配置實作的。
在 application.properties 中添加 debug 屬性,運作配置類,在控制台可以看到:
HelloServiceAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.tyson.service.HelloService' (OnClassCondition)
- @ConditionalOnProperty (hello.enabled) matched (OnPropertyCondition)
HelloServiceAutoConfiguration#helloService matched:
- @ConditionalOnMissingBean (types: com.tyson.service.HelloService; SearchStrategy: all) did not find any beans (OnBeanCondition)
可以在 application.properties 中配置 msg 的内容:
hello.msg=大彬
@Value注解的原理
@Value的解析就是在bean初始化階段。BeanPostProcessor定義了bean初始化前後使用者可以對bean進行操作的接口方法,它的一個重要實作類AutowiredAnnotationBeanPostProcessor為bean中的@Autowired和@Value注解的注入功能提供支援。
Spring Boot 需要獨立的容器運作嗎?
不需要,内置了 Tomcat/ Jetty 等容器。
Spring Boot 支援哪些日志架構?
Spring Boot 支援 Java Util Logging, Log4j2, Lockback 作為日志架構,如果你使用 Starters 啟動器,Spring Boot 将使用 Logback 作為預設日志架構,但是不管是那種日志架構他都支援将配置檔案輸出到控制台或者檔案中。
YAML 配置的優勢在哪裡 ?
YAML 配置和傳統的 properties 配置相比之下,有這些優勢:
- 配置有序
- 簡潔明了,支援數組,數組中的元素可以是基本資料類型也可以是對象
缺點就是不支援 @PropertySource 注解導入自定義的 YAML 配置。
什麼是 Spring Profiles?
在項目的開發中,有些配置檔案在開發、測試或者生産等不同環境中可能是不同的,例如資料庫連接配接、redis的配置等等。那我們如何在不同環境中自動實作配置的切換呢?Spring給我們提供了profiles機制給我們提供的就是來回切換配置檔案的功能
Spring Profiles 允許使用者根據配置檔案(dev,test,prod 等)來注冊 bean。是以,當應用程式在開發中運作時,隻有某些 bean 可以加載,而在 PRODUCTION中,某些其他 bean 可以加載。假設我們的要求是 Swagger 文檔僅适用于 QA 環境,并且禁用所有其他文檔。這可以使用配置檔案來完成。Spring Boot 使得使用配置檔案非常簡單。
SpringBoot多資料源事務如何管理
第一種方式是在service層的@TransactionManager中使用transactionManager指定DataSourceConfig中配置的事務。
第二種是使用jta-atomikos實作分布式事務管理。
spring-boot-starter-parent 有什麼用 ?
新建立一個 Spring Boot 項目,預設都是有 parent 的,這個 parent 就是 spring-boot-starter-parent ,spring-boot-starter-parent 主要有如下作用:
- 定義了 Java 編譯版本。
- 使用 UTF-8 格式編碼。
- 執行打包操作的配置。
- 自動化的資源過濾。
- 自動化的插件配置。
- 針對 application.properties 和 application.yml 的資源過濾,包括通過 profile 定義的不同環境的配置檔案,例如 application-dev.properties 和 application-dev.yml。
Spring Boot 打成的 jar 和普通的 jar 有什麼差別 ?
- Spring Boot 項目最終打包成的 jar 是可執行 jar ,這種 jar 可以直接通過 java -jar xxx.jar 指令來運作,這種 jar 不可以作為普通的 jar 被其他項目依賴,即使依賴了也無法使用其中的類。
- Spring Boot 的 jar 無法被其他項目依賴,主要還是他和普通 jar 的結構不同。普通的 jar 包,解壓後直接就是包名,包裡就是我們的代碼,而 Spring Boot 打包成的可執行 jar 解壓後,在 \BOOT-INF\classes 目錄下才是我們的代碼,是以無法被直接引用。如果非要引用,可以在 pom.xml 檔案中增加配置,将 Spring Boot 項目打包成兩個 jar ,一個可執行,一個可引用。
SpringBoot多資料源拆分的思路
先在properties配置檔案中配置兩個資料源,建立分包mapper,使用@ConfigurationProperties讀取properties中的配置,使用@MapperScan注冊到對應的mapper包中 。
持續關注我!會有更多精彩的Java面試分享!