Skip to content

Commit 9bb6b5c

Browse files
committed
【Spring】Spring高级话题-Spring Aware
1 parent 60661bc commit 9bb6b5c

File tree

2 files changed

+153
-5
lines changed

2 files changed

+153
-5
lines changed

springBoot/.idea/workspace.xml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
#分析
3+
4+
Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的。
5+
6+
也就是说,你可以把你的容器换成别的容器,如Google Guice,这时Bean之间的耦合度很低。
7+
8+
但是在实际项目中,基本上不可避免的要用到Spring容器本身的功能资源,这时你的Bean必须要意识到Spring容器的存在,才能调用Spring所提供的资源,这就是所谓的Spring Aware。
9+
10+
其实Spring Aware本来就是Spring设计用来框架内部使用的,如果使用了Spring Aware,你的Bean就会和Spring框架耦合。也就不能换容器了。
11+
12+
现在把Spring提供的Aware接口列出来:
13+
14+
| Spring提供的Aware接口 ||
15+
| ------------- |:-------------:|
16+
| BeanNameAware | 获得容器中Bean的名称 |
17+
| BeanFactoryAware | 获得当前bean factory,这样可以调用容器的服务 |
18+
| ApplicationContextAware* | 当前的application context,这样可以调用容器的服务 |
19+
| MessageSourceAware | 获得message source,这样可以获得文本信息 |
20+
| ApplicationEventPublisherAware | 应用实践发布器,可以发布事件 |
21+
| ResourceLoaderAware | 获得资源加载器,可以获得外部资源文件 |
22+
23+
Spring Aware的目的是为了让Bean获得Spring容器的服务。
24+
25+
因为ApplicationContext接口集成了MessageSource接口、ApplicationEventPublisher接口和ResourceLoader接口,所以Bean继承ApplicationContextAware可以获得Spring容器的所有服务,但是,原则上我们还是用到什么接口了,就实现什么接口。
26+
27+
在这里的示例,简单的演示BeanNameAware接口和ResourceLoaderAware接口。
28+
29+
一样的,进行本示例的演示,需要先配置好Maven和Spring哦、
30+
见:
31+
<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
32+
33+
#示例
34+
35+
因为要演示外部资源,所以先准备好一个外部文件资源,
36+
我就建在java文件目录下,test.txt文件,内容:
37+
```
38+
测试文件的内容
39+
```
40+
41+
##Spring Aware 演示Bean
42+
43+
```
44+
package cn.hncu.p3.p1_SpringAware;
45+
46+
import org.apache.commons.io.IOUtils;
47+
import org.springframework.beans.factory.BeanNameAware;
48+
import org.springframework.context.ResourceLoaderAware;
49+
import org.springframework.core.io.Resource;
50+
import org.springframework.core.io.ResourceLoader;
51+
import org.springframework.stereotype.Service;
52+
53+
import java.io.IOException;
54+
55+
/**
56+
* Created with IntelliJ IDEA.
57+
* User: 陈浩翔.
58+
* Date: 2016/11/16.
59+
* Time: 下午 6:37.
60+
* Explain:Spring Aware演示Bean
61+
*/
62+
@Service
63+
public class AwareService implements BeanNameAware,ResourceLoaderAware {
64+
//实现BeanNameAware,ResourceLoaderAware接口,获得Bean名称和资源加载的服务
65+
66+
private String beanName;
67+
private ResourceLoader loader;
68+
69+
@Override
70+
public void setResourceLoader(ResourceLoader resourceLoader) {//实现ResourceLoaderAware需要重写setResourceLoader方法
71+
this.loader = resourceLoader;
72+
}
73+
74+
@Override
75+
public void setBeanName(String beanName) {//实现BeanNameAware需要重写setBeanName方法
76+
this.beanName = beanName;
77+
}
78+
79+
public void outputResult(){
80+
System.out.println("Bean的名称为:"+beanName);
81+
Resource resource = loader.getResource("cn/hncu/p3/p1_SpringAware/test.txt");
82+
83+
try {
84+
System.out.println("ResourceLoader加载的文件内容为: "+ IOUtils.toString(resource.getInputStream()));
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
}
88+
89+
}
90+
91+
}
92+
93+
```
94+
95+
##配置类
96+
97+
```
98+
package cn.hncu.p3.p1_SpringAware;
99+
100+
import org.springframework.context.annotation.ComponentScan;
101+
import org.springframework.context.annotation.Configuration;
102+
103+
/**
104+
* Created with IntelliJ IDEA.
105+
* User: 陈浩翔.
106+
* Date: 2016/11/16.
107+
* Time: 下午 6:48.
108+
* Explain:配置类
109+
*/
110+
@Configuration
111+
@ComponentScan("cn.hncu.p3.p1_SpringAware")
112+
public class AwareConfig {
113+
}
114+
115+
```
116+
117+
##运行类
118+
119+
```
120+
package cn.hncu.p3.p1_SpringAware;
121+
122+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
123+
124+
/**
125+
* Created with IntelliJ IDEA.
126+
* User: 陈浩翔.
127+
* Date: 2016/11/16.
128+
* Time: 下午 6:49.
129+
* Explain:运行类
130+
*/
131+
public class Main {
132+
public static void main(String[] args) {
133+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
134+
135+
AwareService awareService = context.getBean(AwareService.class);
136+
awareService.outputResult();
137+
138+
context.close();
139+
}
140+
141+
}
142+
143+
```
144+
145+
#运行结果
146+
147+
![](http://img.blog.csdn.net/20161116193220921)
148+

0 commit comments

Comments
 (0)