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的更多相关文章

  1. spring + spring mvc + tomcat 面试题(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  2. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  3. 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

  4. spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

  5. [转]基于Spring + Spring MVC + Mybatis 高性能web构建

    http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...

  6. Spring+Spring MVC+MyBatis

    Spring+Spring MVC+MyBatis 目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六. ...

  7. MyBatis+Spring+Spring MVC整合开发

    MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...

  8. 基于Spring + Spring MVC + Mybatis 高性能web构建

    基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...

  9. maven/eclipse搭建ssm(spring+spring mvc+mybatis)

    maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实 ...

  10. Spring + Spring MVC + Hibernate

    Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...

随机推荐

  1. 如何修改运行中的docker容器的端口映射

    在docker run创建并运行容器的时候,可以通过-p指定端口映射规则.但是,我们经常会遇到刚开始忘记设置端口映射或者设置错了需要修改.当docker start运行容器后并没有提供一个-p选项或设 ...

  2. HDU 1003 解题报告

    问题描述:求最大连续字串 分析:一道简单的DP,状态转移方程是d[i] = ( d[i-1]+a[i] > a[i] ) ? d[i-1]+a[i] : a[i] d[i]表示以第i个数字结尾的 ...

  3. docker 搭建gitlab

    https://docs.gitlab.com/omnibus/docker/ https://blog.csdn.net/m0_37444820/article/details/81147452 h ...

  4. python实现获取文件夹中的最新文件

    实现代码如下: #查找某目录中的最新文件import osclass FindNewFile: def find_NewFile(self,path): #获取文件夹中的所有文件 lists = os ...

  5. 【ABAP系列】SAP LSWM处理时,网络中断,出现错误

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP LSWM处理时,网络中断 ...

  6. Rust SDL2配置

    github地址 https://github.com/Rust-SDL2/rust-sdl2 clone或下载这个项目 本文使用的是MSVC版本 上面链接页面搜Windows (MSVC) 得知需要 ...

  7. Google File System 论文阅读笔记

    核心目标:Google File System是一个面向密集应用的,可伸缩的大规模分布式文件系统.GFS运行在廉价的设备上,提供给了灾难冗余的能力,为大量客户机提供了高性能的服务. 1.一系列前提 G ...

  8. Java8 Nashorn JavaScript引擎

    使用Java8,Nashorn大大提高了JavaScript 引擎引入,以取代现有的Nashorn Java脚本引擎.Nashorn提供2至10倍更好的性能,因为它直接编译代码在存储器,并传递到字节码 ...

  9. get_date.sh

    #!/usr/bin#####################################################################日期函数处理#获取某个月份的天数 getM ...

  10. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...