Skip to content

Commit 085b5f8

Browse files
committed
【Spring】Spring高级话题-组合注解与元注解
1 parent 5169cf6 commit 085b5f8

File tree

5 files changed

+381
-32
lines changed

5 files changed

+381
-32
lines changed

springBoot/.idea/workspace.xml

Lines changed: 36 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#分析
2+
3+
所谓的元注解:其实就是可以注解到别的注解上的注解。
4+
而被注解的注解我们就称之为组合注解。(仔细理解,可能有点绕)
5+
组合注解具备元注解的功能!
6+
7+
Spring的很多注解都可以作为元注解,而且Spring本身已经有很多组合注解。
8+
9+
比如@Configuration就是一个组合@Component注解,表明这个类其实也是一个Bean。
10+
@Configuration的源码:
11+
```
12+
//
13+
// Source code recreated from a .class file by IntelliJ IDEA
14+
// (powered by Fernflower decompiler)
15+
//
16+
17+
package org.springframework.context.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
import org.springframework.stereotype.Component;
25+
26+
@Target({ElementType.TYPE})
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Documented
29+
@Component
30+
public @interface Configuration {
31+
String value() default "";
32+
}
33+
34+
```
35+
36+
有的时候,我们可能大量同时使用到几个注解到同一个类上,这个时候,我们就可以考虑将这几个注解到别的注解上。
37+
38+
比如下面的示例就是将@Configuration@ComponentScan注解到一个注解上!
39+
40+
这样,我们就可以用一个注解来表示这两个注解。
41+
42+
#示例
43+
44+
##组合注解
45+
46+
```
47+
package cn.hncu.p3.p5_annotation;
48+
49+
import org.springframework.context.annotation.ComponentScan;
50+
import org.springframework.context.annotation.Configuration;
51+
52+
import java.lang.annotation.*;
53+
54+
/**
55+
* Created with IntelliJ IDEA.
56+
* User: 陈浩翔.
57+
* Date: 2016/12/8.
58+
* Time: 下午 4:00.
59+
* Explain:组合注解
60+
*/
61+
62+
@Target(ElementType.TYPE)
63+
@Retention(RetentionPolicy.RUNTIME)
64+
@Documented
65+
@Configuration//组合@Configuration元注解 bean注解
66+
@ComponentScan//组合@ComponentScan元注解 自动扫描对应value(package路径)值下面的所有bean
67+
public @interface WiselyConfiguration {
68+
String[] value() default {};//覆盖value参数
69+
//就是覆盖@ComponentScan注解中的value参数----必须要写,否则组合注解中放入value值时会报错
70+
}
71+
72+
```
73+
解释一下@Documented
74+
表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中.
75+
76+
##服务Bean
77+
78+
```
79+
package cn.hncu.p3.p5_annotation;
80+
81+
import org.springframework.stereotype.Service;
82+
83+
/**
84+
* Created with IntelliJ IDEA.
85+
* User: 陈浩翔.
86+
* Date: 2016/12/8.
87+
* Time: 下午 8:17.
88+
* Explain:服务Bean
89+
*/
90+
@Service
91+
public class DemoService {
92+
public void outputResult(){
93+
System.out.println("从组合注解配置照样获得的bean");
94+
}
95+
}
96+
97+
```
98+
99+
##配置类
100+
101+
现在就只需要我们自定义的那个注解就可以代表那两个注解了。
102+
103+
```
104+
package cn.hncu.p3.p5_annotation;
105+
106+
/**
107+
* Created with IntelliJ IDEA.
108+
* User: 陈浩翔.
109+
* Date: 2016/12/8.
110+
* Time: 下午 8:19.
111+
* Explain:配置类--组合注解
112+
*/
113+
@WiselyConfiguration("cn.hncu.p3.p5_annotation")
114+
//自定义注解,扫描的所有的bean来源于value值所对应的包路径下
115+
//使用@WiselyConfiguration组合注解替代@Configuration和@ComponentScan
116+
public class DemoConfig {
117+
}
118+
119+
```
120+
121+
##运行类
122+
123+
```
124+
package cn.hncu.p3.p5_annotation;
125+
126+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
127+
128+
/**
129+
* Created with IntelliJ IDEA.
130+
* User: 陈浩翔.
131+
* Date: 2016/12/8.
132+
* Time: 下午 8:21.
133+
* Explain:运行类
134+
*/
135+
public class Main {
136+
public static void main(String[] args) {
137+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
138+
DemoService demoService = context.getBean(DemoService.class);
139+
demoService.outputResult();
140+
context.close();
141+
}
142+
}
143+
144+
```
145+
146+
#运行结果
147+
148+
![](http://img.blog.csdn.net/20161208204519300)
149+
437 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)