Skip to content

Commit b88fee2

Browse files
committed
spring import 注解分析
1 parent f0c6bc8 commit b88fee2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [Spring messageSource](/docs/Spring/clazz/Spring-MessageSource.md)
4949
- [Spring 自定义属性解析器](/docs/Spring/clazz/Spring-Custom-attribute-resolver.md)
5050
- [Spring 排序工具](/docs/Spring/clazz/Spring-OrderUtils.md)
51+
- [Spring-import注解](/docs/Spring/clazz/Spring-Import.md)
5152
### Spring5 新特性
5253
- [Spring5-spring.components解析](/docs/Spring/Spring5新特性/Spring-spring-components.md)
5354

docs/Spring/clazz/Spring-Import.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Spring Import
2+
- Author: [HuiFer](https://github.com/huifer)
3+
- 源码阅读仓库: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
4+
5+
## 分析
6+
- org.springframework.context.annotation.Import
7+
```java
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Documented
11+
public @interface Import {
12+
13+
/**
14+
* {@link Configuration @Configuration}, {@link ImportSelector},
15+
* {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
16+
*
17+
* 需要导入的类
18+
*/
19+
Class<?>[] value();
20+
}
21+
```
22+
### ImportBeanDefinitionRegistrar
23+
- 注册Import Bean
24+
- `org.springframework.context.annotation.ImportBeanDefinitionRegistrar`
25+
```java
26+
public interface ImportBeanDefinitionRegistrar {
27+
28+
/**
29+
* Register bean definitions as necessary based on the given annotation metadata of
30+
* the importing {@code @Configuration} class.
31+
* <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
32+
* registered here, due to lifecycle constraints related to {@code @Configuration}
33+
* class processing.
34+
*
35+
* 对import value属性的注册
36+
* @param importingClassMetadata annotation metadata of the importing class
37+
* @param registry current bean definition registry
38+
*/
39+
void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
40+
41+
}
42+
```
43+
- 两个实现类
44+
1. `org.springframework.context.annotation.AutoProxyRegistrar`
45+
2. `org.springframework.context.annotation.AspectJAutoProxyRegistrar`
46+
47+
#### AutoProxyRegistrar
48+
```java
49+
public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
50+
51+
private final Log logger = LogFactory.getLog(getClass());
52+
53+
/**
54+
* 注册import bean定义
55+
*/
56+
@Override
57+
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
58+
boolean candidateFound = false;
59+
// 获取注解
60+
Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
61+
for (String annType : annTypes) {
62+
// 注解属性
63+
AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
64+
if (candidate == null) {
65+
continue;
66+
}
67+
// 获取 mode 属性
68+
Object mode = candidate.get("mode");
69+
// 获取代理对象
70+
Object proxyTargetClass = candidate.get("proxyTargetClass");
71+
if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() &&
72+
Boolean.class == proxyTargetClass.getClass()) {
73+
candidateFound = true;
74+
if (mode == AdviceMode.PROXY) {
75+
// 注册
76+
AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
77+
if ((Boolean) proxyTargetClass) {
78+
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
79+
return;
80+
}
81+
}
82+
}
83+
}
84+
if (!candidateFound && logger.isInfoEnabled()) {
85+
String name = getClass().getSimpleName();
86+
logger.info(String.format("%s was imported but no annotations were found " +
87+
"having both 'mode' and 'proxyTargetClass' attributes of type " +
88+
"AdviceMode and boolean respectively. This means that auto proxy " +
89+
"creator registration and configuration may not have occurred as " +
90+
"intended, and components may not be proxied as expected. Check to " +
91+
"ensure that %s has been @Import'ed on the same class where these " +
92+
"annotations are declared; otherwise remove the import of %s " +
93+
"altogether.", name, name, name));
94+
}
95+
}
96+
97+
}
98+
```

0 commit comments

Comments
 (0)