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) 编辑 收藏 在自 ...
随机推荐
- [CSS]CSS浮动塌陷及解决办法
一. CSS浮动 先看一个例子 <html !DOCTYPE> <head> <title>HTML2</title> <style> .d ...
- R语言rvest包网络爬虫
R语言网络爬虫初学者指南(使用rvest包) 钱亦欣 发表于 今年 06-04 14:50 5228 阅读 作者 SAURAV KAUSHIK 译者 钱亦欣 引言 网上的数据和信息无穷无尽,如 ...
- 移动端调试 — Pure|微信环境调试方案|App环境调试方案
Pure 详细参见: 中文文档:http://leeluolee.github.io/2014/10/24/use-puer-helpus-developer-frontend/ 源码:https:/ ...
- python2.7+RobotFramework的UI自动化环境搭建
robotFramework是一种比较常见的自动化测试框架,此篇记录环境搭建 目录 1.软件准备 2.执行安装 1.软件准备 python-2.7.15.amd64.msi ...
- Gogs 安装 - 本地安装,容器安装
文章目录 安装 Gogs 本地安装 前提条件: 数据库 git 创建 git 用户 SSH 服务器 安装 升级 配置及运行 配置 运行 Gogs 服务 在线安装 Gogs 后台运行 gogs 通过 d ...
- MySQL-第三篇SQL语句基础(2)数据库约束
1.数据库约束. 约束是在表上强制执行的数据校验规则,约束主要用于保证数据库里数据的完整性. MySQL使用information_schema数据库里的TABLE_CONSTRAINTS表来保存该数 ...
- Python入门习题4.文本进度条
例4.1.设置一组文本进度条,使之运行效果如下: --------执行开始--------% 0 [->**********]%10 [*->*********]%20 [**->* ...
- docker pull理解误区
docker run 命令 如果local image中有对应 镜像+tag 不会从新拉取镜像 docker pull 会进行拉取 先进行镜像更改 [root@master01 ~]# docker ...
- mysql处理重复数据仅保留一条记录
目的:去除(或删除)一个表里面手机号重复的数据,但是需要保留其中一个记录,换句话说,表里面手机号不为空的数据,一个手机有且只有一条记录 表结构: CREATE TABLE `account` ( `i ...
- rsync配置教程
本文默认服务器已经安装了 rsync ! 本文默认服务器已经安装了 rsync ! 本文默认服务器已经安装了 rsync ! 切换到 /etc目录,默认情况下,rsyncd.conf 文件如下: # ...