一、Spring Aware

  Spring 依赖注入的最大亮点就是你所有的 Bean 对 Spring容器的存在是没有意识的。即你可以将你的容器替换成其他的容器,如Google Guice,这时 Bean 之间的耦合度很低。

  但在实际项目中,你不可避免的要用到Spring容器本身的功能资源,这时你的 Bean 必须要意识到Spring容器的存在,才能调用Spring所提供的资源,这就是所谓的 Spring Aware。其实 Spring Aware 本来就是 Spring 设计用来框架内部使用的,若使用了 Spring Aware,你的 Bean 将和 Spring框架耦合。

  Spring 提供的 Aware 接口:

  • BeanNameAware          获取容器中Bean的名称
  • BeanFactoryAware            获取当前的 bean factory,这样可以调用容器的服务
  • ApplicationContextAware             获取当前的 application context,这样可以调用容器服务
  • MessageSourceAware               获得 message source,这样可以获得文本信息
  • ApplicationEventPublisherAware   应用事件发布器,可以发布事件,前面提到的 DemoPulisher 也可实现这个接口来发布事件
  • ResourceLoaderAware            获得资源加载器,可以获得外部资源文件

  Spring Aware的目的是为了让 Bean 获得 Spring 容器的服务。因为 ApplicationContext 接口集成了 MessageSource接口、ApplicationEventPublisher接口 和 ResourceLoader接口,所以Bean继承 ApplicationContextAware 就可以获得Spring容器的所有服务,但原则上o们还是用到什么接口,就实现什么接口。

示例:

  1.准备。在aware文件下新建一个test.txt,内容随意,给下面的外部资源加载使用

  2.Spring Aware演示Bean

package com.ecworking.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.Service; import java.io.IOException; @Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{ // 实现BeanNameAware和ResourceLoaderAware接口,获得Bean名称和资源加载的服务 private String beanName; private ResourceLoader loader; @Override
public void setBeanName(String beanName) { // 实现BeanNameAware需重写setBeanName方法
this.beanName = beanName;
} @Override
public void setResourceLoader(ResourceLoader loader) { // 实现ResourceLoaderAware需重写setResourceLoader方法
this.loader = loader;
} public void outputResult(){
System.out.println("Bean的名称为:" + beanName); Resource resource = loader.getResource("classpath:com/ecworking/aware/test.txt"); try {
System.out.println("ResourceLoader加载的文件内容为:" + IOUtils.toString(resource.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
}

  3.配置类

package com.ecworking.aware;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.ecworking.aware")
public class AwareConfig {
}

  4.运行

package com.ecworking.aware;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); AwareService service = context.getBean(AwareService.class); service.outputResult(); context.close();
}
}

运行结果:

Spring Boot实战笔记(五)-- Spring高级话题(Spring Aware)的更多相关文章

  1. Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的便利,是的,我也是这样认为的,I know your felling . 前期了解概念什么的确实 ...

  2. Spring Boot实战笔记(一)-- Spring简介

    一.Spring 概述 Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓的解决方案就是可以基于Spring解决所有的Java EE开发的所有问题. Spring框架主要提供了Ioc(In ...

  3. JavaEE开发的颠覆者 Spring Boot实战--笔记

    1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...

  4. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  5. Spring Boot学习笔记(五)整合mybatis

    pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...

  6. spring boot 实战笔记(一)

    spring 概述: Bean :每一个被 Spring 管理的 JAVA对象,都称之为 Bean.Spring提供一个IoC容器来初始化对象,负责创建Bean, 解决对象之间的依赖管理和对象的使用. ...

  7. spring boot学习笔记(二)创建spring boot项目

    用eclipse(需要用高版本,要不然弄不出来):new →Spring Sarter Project 用IDEA:一般默认 一般默认 入门级的先 剩下的一般默认... 一.项目至少有下面的东西,里面 ...

  8. Spring Boot实战笔记(七)-- Spring高级话题(计划任务)

    一.计划任务 从Spring3.1开始,计划任务在Spring中的实现变得异常的简单.首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在执行计划任务的方法上注解@Sc ...

  9. Spring Boot实战笔记(九)-- Spring高级话题(组合注解与元注解)

    一.组合注解与元注解 从Spring 2开始,为了响应JDK 1.5推出的注解功能,Spring开始大量加入注解来替代xml配置.Spring的注解主要用来配置注入Bean,切面相关配置(@Trans ...

随机推荐

  1. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

  2. Web应用程序设计十个建议

    原文链接:  Top 10 Design Tips for Web Apps 原文日期: 2014年04月02日 翻译日期: 2014年04月11日 翻译人员: 铁锚 现代web应用通常在互联网上通过 ...

  3. LeetCode之“字符串”:ZigZag Conversion

    题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  4. ORACLE ERP各模块会计分录

      ORACLE ERP各模块会计分录   第一章 采购模块 一.资产采购(科目来源:库存组织) 1.物料接收 借  材料采购     接收数量*采购单价 贷 应计暂估     接收数量*采购单价 2 ...

  5. vs2010 matlab混合编程调用matlab引擎

    // matlab_engine.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "engine.h" ...

  6. ios的位置和方向(来自苹果官方文档,仅供简单参考)

    取得用户的当前位置 Core Location框架使您可以定位设备的当前位置,并将这个信息应用到程序中.该框架利用设备内置的硬件,在已有信号的基础上通过三角测量得到固定位置,然后将它报告给您的代码.在 ...

  7. 图像处理程序框架—MFC相关知识点

    CDC:Windows使用与设备无关的图形设备环境(DC :Device Context) 进行显示 . MFC基础类库定义了设备环境对象类----CDC类. CDC与CGdiObject的关系 说道 ...

  8. linux下创建且挂载光盘镜像

    在linux下可以很方便的将多个文件,或多个文件夹下的内容打包进光盘镜像中,我们可以用: mkisofs -r -v -o xxx.iso /root /home 命令将/root以及/home目录下 ...

  9. Nginx使用图片处理模块

    Nginx可以编写很多额外的模块,这里我们需要按照能够通过URL响应返回缩放且含图片水印功能的模块. 1.安装一些使用过程中会用到的工具 yum install libgd2-devel yum in ...

  10. css左侧固定宽度右侧自适应

    左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 要求不用JS或CSS行为实现: 仔细分析试题要求,要达到 ...