前言
最近在學習Spring Boot相關的課程,過程中以筆記的形式記錄下來,方便以后回憶,同時也在這里和大家探討探討,文章中有漏的或者有補充的、錯誤的都希望大家能夠及時提出來,本人在此先謝謝了!
開始之前呢,希望大家帶着幾個問題去學習:
1、Spring注解驅動是什么?
2、這個功能在什么時代背景下發明產生的?
3、這個功能有什么用?
4、怎么實現的?
5、優點和缺點是什么?
6、這個功能能應用在工作中?
這是對自我的提問,我認為帶着問題去學習,是一種更好的學習方式,有利於加深理解。好了,接下來進入主題。
1、起源
我們先來簡單的聊聊Spring注解的發展史。Spring1.x時代,那時候注解的概念剛剛興起,僅支持如 @Transactional 等注解。到了2.x時代Spring的注解體系有了雛形,引入了 @Autowired 、 @Controller 這一系列骨架式的注解。3.x是黃金時代,它除了引入 @Enable 模塊驅動概念,加快了Spring注解體系的成型,還引入了配置類 @Configuration 及 @ComponentScan ,使我們可以拋棄XML配置文件的形式,全面擁抱Spring注解,但Spring並未完全放棄XML配置文件,它提供了 @ImportResource 允許導入遺留的XML配置文件。此外還提供了 @Import 允許導入一個或多個Java類成為Spring Bean。4.X則趨於完善,引入了條件化注解 @Conditional ,使裝配更加的靈活。當下是5.X時代,是SpringBoot2.0的底層核心框架,目前來看,變化不是很大,但也引入了一個 @Indexed 注解,主要是用來提升啟動性能的。好了,以上是Spring注解的發展史,接下來我們對Spring注解體系的幾個議題進行講解。
2、Spring 模式注解
模式注解是一種用於聲明在應用中扮演“組件”角色的注解。如 Spring 中的 @Repository 是用於扮演倉儲角色的模式注解,用來管理和存儲某種領域對象。還有如@Component 是通用組件模式、@Service 是服務模式、@Configuration 是配置模式等。其中@Component 作為一種由 Spring 容器托管的通用模式組件,任何被 @Component 標注的組件均為組件掃描的候選對象。類似地,凡是被 @Component 標注的注解,如@Service ,當任何組件標注它時,也被視作組件掃描的候選對象。
舉例:
Spring注解 | 場景說明 | 起始版本 |
---|---|---|
@Componnt | 通用組件模式注解 | 2.5 |
@Repository | 數據倉儲模式注解 | 2.0 |
@Service | 服務模式注解 | 2.5 |
@Controller | Web 控制器模式注解 | 2.5 |
@Configuration | 配置類模式注解 | 3.0 |
那么,被這些注解標注的類如何交由Spring來管理呢,或者說如何被Spring所裝配呢?接下來我們就來看看Spring的兩種裝配方式。
2.1、裝配方式
-
<context:component-scan> 方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.loong.spring.boot" />
</beans>
第一種是XML配置文件的方式,通過 base-package 這個屬性指定掃描某個范圍內所有被 @Component 或者其派生注解標記的類(Class),將它們注冊為 Spring Bean。
我們都知道XML Schema 規范,標簽需要顯示地關聯命名空間,如配置文件中的 xmlns:context="http://www.springframework.org/schema/context" ,且需要與其處理類建立映射關系,而該關系維護在相對於 classpath 下的/META-INF/spring.handlers 文件中。如下:
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
可以看到, context 所對應的處理器為 ContextNamespaceHandler
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
.....
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
.....
}
}
這里當Spring啟動時,init方法被調用,隨后注冊該命名空間下的所有 Bean 定義解析器,可以看到 <context:component-scan /> 的解析器為 ComponentScanBeanDefinitionParser 。具體的處理過程就在此類中,感興趣的同學可以去深入了解,這里不再贅述。
-
@ComponentScan 方式
@ComponentScan(basePackages = "com.loong.spring.boot")
public class SpringConfiguration {
}
第二種是注解的形式,同樣也是依靠 basePackages 屬性指定掃描范圍。
Spring 在啟動時,會在某個生命周期內創建所有的配置類注解解析器,而 @ComponentScan 的處理器為 ComponentScanAnnotationParser ,感興趣的同學可以去深入了解,這里同樣不再贅述。
2.2、派生性
我們用自定義注解的方式來看一看文中提到的派生性:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repository
public @interface FirstLevelRepository {
String value() default "";
}
可以看到我們自定義了一個
@FirstLevelRepository 注解,當前注解又標注了 @Repository,而 @Repository 又標注了 @Component 並且注解屬性一致(String value() default""),那么就可以表示當前注解包含了
@Repository 及 @Component 的功能。
派生性其實可以分為多層次的,如
@SprintBootApplication -> @SpringBootConfiguration -> @Configuration -> @Component 可以看到@Component被派生了多個層次,但這種多層次的派生性Spring 4.0版本才開始支持,Spring3.0僅支持兩層。
3、Spring @Enable 模塊驅動
前文提到Spring3.X是一個黃金時代,它不僅全面擁抱注解模式,還開始支持“@Enable模塊驅動”。所謂“模塊”是指具備相同領域的功能組件集合,組合所形成的一個獨立的單元,比如 Web MVC 模塊、AspectJ代理模塊、Caching(緩存)模塊、JMX(Java 管理擴展)模塊、Async(異步處理)模塊等。這種“模塊”理念在后續的Spring 、Spring Boot和Spring Cloud版本中都一直被使用,這種模塊化的注解均以 @Enable 作為前綴,如下所示:
框架實現 | @Enable注解模塊 | 激活模塊 |
---|---|---|
Spring Framework | @EnableWebMvc | Web Mvc 模塊 |
/ | @EnableTransactionManagement | 事物管理模塊 |
/ | @EnableWebFlux | Web Flux 模塊 |
Spring Boot | @EnableAutoConfiguration | 自動裝配模塊 |
/ | @EnableConfigurationProperties | 配置屬性綁定模塊 |
/ | @EnableOAuth2Sso | OAuth2 單點登陸模塊 |
Spring Cloud | @EnableEurekaServer | Eureka 服務器模塊 |
/ | @EnableFeignClients | Feign 客戶端模塊 |
/ | @EnableZuulProxy | 服務網關 Zuul 模塊 |
/ | @EnableCircuitBreaker | 服務熔斷模塊 |
引入模塊驅動的意義在於簡化裝配步驟,屏蔽了模塊中組件集合裝配的細節。但該模式必須手動觸發,也就是將該注解標注在某個配置Bean中,同時理解原理和加載機制的成本較高。那么,Spring是如何實現 @Enable 模塊呢?主要有以下兩種方式。
3.1、Spring框架中@Enable實現方式
-
基於 @Import 注解
首先,參考 @EnableWebMvc 的實現:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...
}
這種實現模式主要是通過 @Import 導入配置類 DelegatingWebMvcConfiguration ,而該類標注了 @Configuration 注解,表明這是個配置類,我們都知道 @EnableWebMvc 是用來激活Web MVC模塊,所以如HandlerMapping 、HandlerAdapter這些和MVC相關的組件都是在這個配置類中被組裝,這也就是所謂的模塊理念。
-
基於接口編程
基於接口編程同樣有兩種實現方式,第一種參考 @EnableCaching的實現:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(CachingConfigurationSelector.class)
public @interface EnableCaching {
...
}
public class CachingConfigurationSelector extends AdviceModeImportSelector<EnableCaching> {
@Override
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) { //switch語句選擇實現模式
case PROXY:
return new String[]{AutoProxyRegistrar.class.getName(), ProxyCachingConfiguration.class.getName()};
case ASPECTJ:
return new String[]{AnnotationConfigUtils.CACHE_ASPECT_CONFIGURATION_CLASS_NAME};
default:
}
}
}
這種方式主要是繼承 ImportSelector 接口(AdviceModeImportSelector實現了ImportSelector接口),然后實現 selectImports 方法,通過入參進而動態的選擇一個或多個類進行導入,相較於注解驅動,此方法更具有彈性。
第二種參考 @EnableApolloConfig 的實現:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(ApolloConfigRegistrar.class)
public @interface EnableApolloConfig {
....
}
public class ApolloConfigRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
....
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(),
PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues);
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(),
PropertySourcesProcessor.class);
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(),
ApolloAnnotationProcessor.class);
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(), SpringValueProcessor.class);
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueDefinitionProcessor.class.getName(), SpringValueDefinitionProcessor.class);
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(),
ApolloJsonValueProcessor.class);
}
}
這種方式主要是通過 @Import 導入實現了 ImportBeanDefinitionRegistrar 接口的類,在該類中重寫 registerBeanDefinitions 方法,通過 BeanDefinitionRegistry 直接手動注冊和該模塊相關的組件。接下來,我們用這兩種方式實現自定義的 @Enable 模塊。
3.2、自定義@Enable模塊實現
-
基於 @Import 注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}
@Configuration
public class HelloWorldConfiguration {
// 可以做一些組件初始化的操作。
@Bean
public String helloWorld(){
return "hello world";
}
// ....
}
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class)
.web(WebApplicationType.NONE).run(args);
String helloWorld = context.getBean("helloWorld",String.class);
System.out.println(helloWorld );
}
}
這里我們自定義了一個 @EnableHelloWorld 注解,再用 @Import 導入一個自定義的配置類 HelloWorldConfiguration,在這個配置類中初始化 helloWorld 。
-
基於接口編程
第一種基於 ImportSelector 接口:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}
public class HelloWorldImportSelector implements ImportSelector {
/**
* 這種方法比較有彈性:
* 可以調用importingClassMetadata里的方法來進行條件過濾
* 具體哪些方法參考:https://blog.csdn.net/f641385712/article/details/88765470
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
if (importingClassMetadata.hasAnnotation("com.loong.case3.spring.annotation.EnableHelloWorld")) {
return new String[]{HelloWorldConfiguration.class.getName()};
}
}
}
@Configuration
public class HelloWorldConfiguration {
// 可以做一些組件初始化的操作
@Bean
public String helloWorld(){
return "hello world";
}
// ....
}
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class)
.web(WebApplicationType.NONE).run(args);
String helloWorld = context.getBean("helloWorld",String.class);
System.out.println(helloWorld );
}
}
這里我們同樣是自定義 @EnableHelloWorld 注解,通過 @Import 導入 HelloWorldImportSelector 類,該類實現了 ImportSelector 接口,在重寫的方法中通過 importingClassMetadata.hasAnnotation("com.loong.case3.spring.annotation.EnableHelloWorld") 判斷該類是否標注了 @EnableHelloWorld 注解,從而導入 HelloWorldConfiguration 類,進行初始化工作。
第二種基於 ImportBeanDefinitionRegistrar 接口:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldRegistrar.class)
public @interface EnableHelloWorld {
}
public class HelloWorldRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
if (annotationMetadata.hasAnnotation("com.loong..case4.spring.annotation.EnableHelloWorld")) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(HelloWorldConfiguration.class);
beanDefinitionRegistry.registerBeanDefinition(HelloWorldConfiguration.class.getName(), beanDefinition);
}
}
}
@Configuration
public class HelloWorldConfiguration {
public HelloWorldConfiguration() {
System.out.println("HelloWorldConfiguration初始化....");
}
}
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class)
.web(WebApplicationType.NONE).run(args);
}
}
這里就是在 HelloWorldRegistrar 中利用 BeanDefinitionRegistry 直接注冊 HelloWorldConfiguration。
4、Spring 條件裝配
條件裝配指的是通過一些列操作判斷是否裝配 Bean ,也就是 Bean 裝配的前置判斷。實現方式主要有兩種:@Profile 和 @Conditional,這里我們主要講 @Conditional 的實現方式,因為 @Profile 在 Spring 4.0 后也是通過 @Conditional 來實現。
@Conditional(HelloWorldCondition.class)
@Component
public class HelloWorldConfiguration {
public HelloWorldConditionConfiguration (){
System.out.println("HelloWorldConfiguration初始化。。。");
}
}
public class HelloWorldCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
// ...
return true;
}
}
這里通過自定義一個 HelloWorldConfiguration 配置類,再標注 @Conditional 注解導入 HelloWorldCondition類,該類必須實現 Condition 接口,然后重寫 matches 方法,在方法中可以通過兩個入參來獲取一系列的上下文數據和元數據,最終返回ture或false來判定該類是否初始化,
5、總結
關於Spring注解驅動的概念就告一段落,最后來簡單的回顧下這篇文章的內容,這篇文章主要講了 Spring 注解相關的幾個概念:Spring模式注解、@Enable 模塊驅動和 Spring 的條件裝配。其中 Spring 模式注解的核心是 @Component,所有的模式注解均被它標注,而對應兩種裝配方式其實是尋找 @Component 的過程。Spring @Enable 模塊的核心是在 @Enable 注解上通過 @Import 導入配置類 ,從而在該配置類中實現和當前模塊相關的組件初始化工作。可以看到,Spring 組件裝配並不具備自動化,都需要手動標注多種注解,且之間需相互配合,所以下一章我們就來講講 Spring Boot是如何基於 Spring 注解驅動來實現自動裝配的。
以上就是本章的內容,如過文章中有錯誤或者需要補充的請及時提出,本人感激不盡。
參考:
《Spring Boot 編程思想》