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. android开发里跳过的坑——GridView使用Glide加载图片不显示

    用grideview显示本地图片列表,用了Glide加载框架,具体调用如下: Glide.with(mContext).load(Uri.fromFile(file)).into(imageView) ...

  2. C# WinForm 中Label自动换行 解决方法

    在TableLayoutPannel中放着一些Label如果把Label的AutoSize属性设成True的话,文字超过label长度时就会自动增加,直到后面的字出窗体以外设置成False时,一旦到达 ...

  3. Oracle数据库字符集问题

    Oracle数据库的字符集问题,也涉及作为服务器操作系统的CentOS或者Windows的字符集与Oracle字符集之间的关联关系Oracle的字符集,这个问题的提出是因为两个原因:一是遇到一个DMP ...

  4. 一个最最简单的 log4j 的 入门级使用案例

    看了比较多的文档和博客,感觉这篇博客写得比较好,比较容易懂,先 mark 一下,回头做一个记录. 文章1:http://www.cnblogs.com/rushoooooo/archive/2011/ ...

  5. laravel的使用

    1.先下载composer.phar 下载地址:https://getcomposer.org/download/ 把composer.phar拷贝到自己的项目目录中,执行以下代码: php comp ...

  6. JavaScript面向对象初步认识

    一.面向对象初步认识 1.什么是对象? 一句话解释: 万物皆对象 对象是有属性的:用{ }来写入! 2.对象的应用(接口) json 接口简单的说就是后台提供给前端提供数据的,让我们进行渲染! 请求接 ...

  7. python保存selenium的cookies写入和读出

    def write_cookie(self, cookie): try: with open("cookies%s" % self.uid, "wb+") as ...

  8. Dpr ppi 适配 等概念 弹性属性的讲解

    Dpr: Dpr的全称(Device pixel ratio)像素设备比例:就是说每个设备像素上占有的css位像素的个数 苹果手机常见的设备像素比:1.0安卓 iPhone2.0  3.0 如果是1. ...

  9. statistic_action

    方差variance 统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数.V 离差平方和(Sum of Squares of Deviations)是各项与平均项之差的平方的 ...

  10. JAVA数组的toString()方法不能直接输出数组内容?

    问题描述:我定义了一个类,类名是Job,当我输出Job.toString()是可以按我重载的toString方法输出的,但是如果输出jobs[]这个数组时,只会输出[Lmodel.Job;@45e22 ...