1 註解
1.1 定義
Annotation(註解),用於爲Java代碼提供元數據。簡單理解註解可以看做是一個個標籤,用來標記代碼。是一種應用於類、方法、參數、變量、構造器及包的一種特殊修飾符。
1.2 註解的聲明
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnnotation{
}
1.3 元註解
元註解就是註解到註解上的註解,或者說元註解是一種基本註解,它能用來註解其他註解。
我們可以將元註解看成一種特殊的修飾符,用來解釋說明註解,它是註解的元數據。
- @Documented
被@Documented修飾的Annotation類將會被javadoc工具提取成文檔。
- @Inherited
被@Inherited修改的Annotation將具有繼承性,如果某個類使用了@MyAnnotation註解(定義該Annotation時使用了@Inherited修飾)修飾,則其子類將自動被@MyAnnotation修飾。
- @Retention
被@Retention修改的註解,結合RetentionPolicy.XXX可以指定該註解存在的聲明週期。
SOURCE:僅存在Java源碼文件,經過編譯器後便丟棄
CLASS:存在Java源文件,以及經過編譯器後生成的Class字節碼文件,但在運行時JVM中不再保留
RUNTIME:存在源文件、變異生成的Class字節碼文件,以及保留在運行時JVM中,可以通過反射讀取註解信息
- @Target
表示該註解類型所使用的程序元素類型,結合ElementType.XXX來使用。
- @Repeatable
Java8新增的可重複註解。
1.4 JDK中常見註解
- @Override
用於告知編譯器,我們需要覆寫超類的當前方法。
- @Deprecated
使用這個註解,用於告知編譯器,某一程序元素(比如方法,成員變量)不建議使用了(即過時了)。
- @SuppressWarnings
用於告知編譯器忽略特定的警告信息,例在泛型中使用原生數據類型,編譯器會發出警告,當使用該註解後,則不會發出警告。
- @FunctionalInterface
用戶告知編譯器,檢查這個接口,保證該接口是函數式接口,即只能包含一個抽象方法,否則就會編譯出錯。
1.5 自定義註解使用
- 格式
@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotataion{
String name();
int age() default 17;
String hello() default "spring boot";
}
- 成員變量
定義:用無形參的方法形式來聲明,註解方法不能帶參數,比如name(),age()
類型:前面可以用數據類型來修飾
默認值:成員變量可以有默認值,比如default "spring boot"
- 註解使用
@MyAnnotation(name="Jack",age=16)
public class Person {
}
- 反射獲取類上的註解
1.獲取需要解析註解的類
Class<Person> clazz=Person.class;
//2.判斷該類上是否有註解
if(clazz.isAnnotationPresent(MyAnnotation.class)){
//3.獲取該類上的註解
MyAnnotation myAnnotation=clazz.getAnnotation(MyAnnotation.class);
//4.打印出註解上的內容
System.out.println(myAnnotation.name()+":"+myAnnotation.age());
}
2 @SpringBootApplication
官網見:18. Using the @SpringBootApplication Annotation
The @SpringBootApplication
annotation is equivalent to using @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
with their default attributes
@SpringBootConfiguration等同於@Configuration
3 @SpringBootConfiguration
@SpringBootConfiguration等同於@Configuration,@Configuration等同於@Component
3.1 作用
@SpringBootConfiguration繼承自@Configuration,二者功能也一致,標註當前類是配置類。
並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例納入到Spring容器中,實例名就是方法名。
3.2 使用
-
@Configuration
: allow to register extra beans in the context or import additional configuration classes
@SpringBootConfiguration
public class Config{
@Bean
public Map getMap(){
Map map=new HashMap();
map.put("username","Jack");
return map;
}
}
可以直接通過context.getBean("getMap")的方式獲取。
3.3 擴展
@Configuration等同與@Component
官網見[Spring Framework Core]:1.10.1. @Component
and Further Stereotype Annotations
conclusion
:@Component includes @Configuration,@Repository,@Service and @Controller
4 @ComponentScan
-
@ComponentScan
: enable@Component
scan on the package where the application is located (see the best practices)
官網見[Spring Framework Core]:1.10.3. Automatically Detecting Classes and Registering Bean Definitions
To autodetect these classes and register the corresponding beans, you need to add @ComponentScan to your @Configuration class, where the basePackages attribute is a common parent package for the two classes. (Alternatively, you can specify a comma- or semicolon- or space-separated list that includes the parent package of each class.)</pre>
@ComponentScan主要就是定義掃描的路徑以及子路徑中,找出標識了需要裝配的類自動裝配到Spring的bean容器中。
官網見:17. Spring Beans and Dependency Injection
If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan
without any arguments. All of your application components (@Component
, @Service
, @Repository
, @Controller
etc.) are automatically registered as Spring Beans.
5 @EnableAutoConfiguration
-
@EnableAutoConfiguration
: enable Spring Boot’s auto-configuration mechanism
官網見: 11.3.2 The @EnableAutoConfiguration Annotation
The second class-level annotation is @EnableAutoConfiguration
. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web
added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
5.1 @Import(XXX)
藉助AutoConfigurationImportSelector,@EnableAutoConfiguration可以幫助Spring Boot應用將所有符合條件的@Configuration配置都加載到IoC容器中
5.2 SpringFactoriesLoader
- selectImports方法
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
autoConfigurationMetadata, annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
- 讀取候選裝配組件getCandidateConfigurations
protected AutoConfigurationEntry getAutoConfigurationEntry(
AutoConfigurationMetadata autoConfigurationMetadata,
AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
Assert.notEmpty(configurations,
"No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
- SpringFacotriesLoader.loadFactoryNames
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
點開loadSpringFactories
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
點開FACTORIES_RESOURCE_LOCATION
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
5.3 autoconfigure.jar
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener</pre>
6 @Conditional
- Indicates that a component is only eligible for registration when all
- {@linkplain #value specified conditions} match.</pre>
6.1 Understanding Auto-Configured Beans
官網見:49.1 Understanding Auto-configured Beans
Under the hood, auto-configuration is implemented with standard
@Configuration
classes. Additional@Conditional
annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use@ConditionalOnClass
and@ConditionalOnMissingBean
annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own@Configuration
.
You can browse the source code of
spring-boot-autoconfigure
to see the@Configuration
classes that Spring provides (see theMETA-INF/spring.factories
file).
6.2 Condition Annotations
官網見:49.3 Condition Annotations
You almost always want to include one or more
@Conditional
annotations on your auto-configuration class. The@ConditionalOnMissingBean
annotation is one common example that is used to allow developers to override auto-configuration if they are not happy with your defaults.Spring Boot includes a number of
@Conditional
annotations that you can reuse in your own code by annotating@Configuration
classes or individual@Bean
methods. These annotations include:
翻譯
:不同類型的Conditional
(1)ConditionalOnClass:當且僅當ClassPath存在指定的Class時,才創建標記上該註解的類的實例
(2)ConditionalOnBean: 當且僅當指定的bean classes and/or bean names在當前容器中,才創建標記上該註解的類的實例
(3)ConditionalOnProperty:當且僅當Application.properties存在指定的配置項時,創建標記上了該註解的類的實例
(4)ConditionalOnResource:在classpath下存在指定的resource時創建
(5)ConditionalOnWebApplication:在web環境下創建