Spring---Spring Aware
1、概述
1.1、Spring中的 所有Bean 对Spring容器的存在 是没有意识的(即你可以将容器换成别的容器,这样使用容器与Bean之间的耦合度很低);
但在实际项目中,不可避免的要用到Spring容器本身的功能资源,这时Bean必须意识到Spring容器的存在,才能调用Spring提供的资源,这就是Spring Aware;
(Spring Aware本身就是Spring设计用来框架内部使用的,若使用了Spring Aware,你的Bean将会与Spring框架耦合)
1.2、案例
package com.an.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 17:27
* @since:
*/
@Configuration
@ComponentScan(value = "com.an")
public class AwareConfig { }
package com.an.aware; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component; /**
* @description: 实现BeanNameAware,ResourceLoaderAware接口,获得Bean名称服务、资源加载服务
* @author: anpeiyong
* @date: Created in 2019/11/19 17:20
* @since:
*/
@Component
public class AwareService implements BeanNameAware, ResourceLoaderAware { private String beanName;
private ResourceLoader resourceLoader; @Override
public void setBeanName(String beanName) {
this.beanName=beanName;
} @Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader=resourceLoader;
} public void outputResult(){
System.out.println("BeanName:"+beanName);
Resource resource =resourceLoader.getResource("classpath:test.properties");
try {
System.out.println("ResourceLoader加载的文件内容:");
System.out.println(IOUtils.toString(resource.getInputStream()));
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.an.main; import com.an.aware.AwareService;
import com.an.config.AwareConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 17:28
* @since:
*/
public class AwareMainTest { public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(AwareConfig.class);
AwareService awareService=annotationConfigApplicationContext.getBean(AwareService.class);
awareService.outputResult();
annotationConfigApplicationContext.close();
} }
结果:
ResourceLoader加载的文件内容:
book.name=jackson
book.author=rose
Spring---Spring Aware的更多相关文章
- spring + spring mvc + tomcat 面试题(史上最全)
文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...
- spring spring data jpa save操作事务
整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...
- 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建
一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...
- spring + spring mvc + mybatis + react + reflux + webpack Web工程例子
前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...
- [转]基于Spring + Spring MVC + Mybatis 高性能web构建
http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...
- Spring+Spring MVC+MyBatis
Spring+Spring MVC+MyBatis 目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六. ...
- MyBatis+Spring+Spring MVC整合开发
MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...
- 基于Spring + Spring MVC + Mybatis 高性能web构建
基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...
- maven/eclipse搭建ssm(spring+spring mvc+mybatis)
maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实 ...
- Spring + Spring MVC + Hibernate
Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...
随机推荐
- C# windows窗口应用程序切换主界面的显示内容
不知道说清楚没有?就是我的窗口分为两部分,左边,控制部分,由一些按钮组成右边,显示部分,由些控件(如下拉,文本等等组成) 左边的每个按钮对应显示部分的页面,也就是说,左边换一个按钮点,那么右边就显示其 ...
- spring cloud网关gateway
spring gateway使用基于netty异步io,第二代网关:zuul 1使用servlet 3,第一代网关,每个请求一个线程,同步Servlet,多线程阻塞模型.而spring貌似不想在支持z ...
- maven 依赖调解
项目A有两条依赖关系 A->B->C->X(1.0),A->D->X(2.0) ,X是A的传递性依赖,但是两条路径上有两个版本的依赖,会选择哪个呢? maven 依赖调 ...
- How to resolve error “Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jre7…” when building in Android Studio Ask Question
//implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation & ...
- stl vector创建二维数组
vector<vector<); for (auto it = v.begin(); it != v.end(); it++) { ; (*it).reserve();//预留空间为5,但 ...
- 网络流强化-HDU2732
第一次遇到加了“多余”的边会导致WA的——在我看来是很多余,见代码191行 之后会思考为什么,想出来再更. 问题弄明白了,如果你在连接边连了一条到没有柱子的点的边,这个没有柱子的点是不可能连到终点的, ...
- Logistic Regression Algorithm解决分类问题
在线性回归算法中,我们看到,在training set中,输入矩阵X与向量y的值都是连续的.所以在二维空间中,我们可以用一条直线去模拟X与y的变化关系,寻找参数向量theta的取值.如根据房屋面积预测 ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- Show Me the Code
最近在练习写Python代码,拥有150多道程序员面试题的LeetCode注重算法的实现,锻炼思维,还能在线测试代码的正确性,而Python练习册涉及到了Python实际的应用,锻炼解决问题的能力,托 ...
- vscode内置c++ define选项
https://stackoverflow.com/questions/46415076/how-can-i-define-macros-for-the-c-intellisense-engine