12.1 Aware

  • 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源;
  • 使用aware能让我们在应用的任意位置获得spring容器的资源;
  • 我们通过实现aware接口来识别spring容器的资源;
  • Spring包含的aware有:
    • BeanNameAware
    • BeanFactoryAware
    • ApplicationContextAware
    • MessageSourceAware
    • ApplicationEventPublisherAware
    • ResourceLoaderAware
  • 实现ApplicationContextAware接口,可识别所有的资源,但最好是你用到什么就使用什么;
  • 这也就意味着我们就耦合到了spring框架上了,没有spring框架你的代码将无法执行;

12.2 示例

12.2.1 新建演示bean

package com.wisely.aware;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements BeanNameAware,ResourceLoaderAware{
private String name;
private ResourceLoader loader; //BeanNameAware接口的方法
public void setBeanName(String beanName) {
this.name = beanName; } //ResourceLoaderAware接口的的方法
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader; } public String getName() {
return name;
} public ResourceLoader getLoader() {
return loader;
} }

12.2.2 新建演示用文件info.txt

jhkljhlkjhlkj
111111111111

12.2.3 测试

package com.wisely.aware;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; public class Main { public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.aware");
DemoBean db = context.getBean(DemoBean.class);
System.out.println(db.getName());
ResourceLoader rl = db.getLoader();
Resource r = rl.getResource("classpath:com/wisely/aware/info.txt");
System.out.println(IOUtils.toString(r.getInputStream()));
context.close();
} }

输出结果

demoBean
jhkljhlkjhlkj
111111111111

12点睛Spring4.1-Spring Aware的更多相关文章

  1. spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware

    aware:英 [əˈweə(r)] 美 [əˈwer] adj.意识到的;知道的;觉察到的 XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX ...

  2. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  3. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  4. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

  5. Spring Boot实战笔记(五)-- Spring高级话题(Spring Aware)

    一.Spring Aware Spring 依赖注入的最大亮点就是你所有的 Bean 对 Spring容器的存在是没有意识的.即你可以将你的容器替换成其他的容器,如Google Guice,这时 Be ...

  6. Spring Aware容器感知技术

    Spring Aware是什么 Spring提供Aware接口能让Bean感知Spring容器的存在,即让Bean可以使用Spring容器所提供的资源. Spring Aware的分类 几种常用的Aw ...

  7. Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍

    Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍 spring集成 mybatis Spring4.x零配置框架搭建 两年前一直在做后 ...

  8. spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务

    Spring Aware的目的是为了让Bean获得Spring容器的服务 //获取容器中的bean名称import org.springframework.beans.factory.BeanName ...

  9. Spring Aware

    spring依赖注入的最大亮点就是所有的bean感知不到spring容器的存在,但在实际开发中,我们不可避免的要用到spring容器本身的功能资源,这时,我们就必须意识到容器的存在(废话,都要跟容器进 ...

随机推荐

  1. Codeforces Round #604 (Div. 2) A,B,C【D题待补】

    思路:直接暴力判断就OK了 #include<bits/stdc++.h> using namespace std; #define int long long signed main() ...

  2. 多路选择器,加法器原理及verilog实现

    1.数据选择器是指经过选择,把多个通道的数据传到唯一的公共数据通道上.实现数据选择功能的逻辑电路称为数据选择器,它的作用相当于多个输入的单刀多掷开关.本例程以四选一数据选择器(电平触发)为例. 四选一 ...

  3. (32)Vue模板语法

    模板语法 文本: <span>Message: {{ msg }}</span> v-once 一次性地插值,当数据改变时,插值处的内容不会更新 <span v-once ...

  4. Linux 系统安装下安装 mysql5.7(glibc版)

    转自:https://www.cnblogs.com/mujingyu/p/7689116.html 前言:经过一天半的折腾,终于把 mysql 5.7.17 版本安装上了 centos 7 系统上, ...

  5. const关键字与数组、指针

    目录 const关键字 const修饰数组 const修饰指针 用两个const修饰指针 @ 开始回顾C基础知识.C中使用指针是很危险的事情,一个不慎就会造成程序崩溃,因此对于传入函数的参数进行保护就 ...

  6. P5025 [SNOI2017]炸弹

    原题链接  https://www.luogu.org/problem/P5025 闲话时刻: 第一道 AC 的黑题,虽然众人皆说水... 其实思路不是很难,代码也不是很难打,是一些我们已经学过的东西 ...

  7. content="IE=Edge"是什么意思?

    永远以最新的IE版本模式来显示网页 <meta http-equiv="X-UA-Compatible" content="IE=7">#以上代码告 ...

  8. gitconfig别名配置

    vim ~/.gitconfig 进行配置 [user] name = Your Name email = you@yourdomain.example.com [core] editor = vim ...

  9. 优雅地使用eruda在移动端上调试网页

    注:本文有一小部分截图为桌面端,但所有内容都亲测在移动端上可用. 你曾否在洛谷上用<F12>把错的数据点改成AC?是否有过自己尝试搭建网站,在移动端上js代码运行出错?如果你的回答是肯定的 ...

  10. UDP如何实现可靠传输

    概述 UDP不属于连接协议,具有资源消耗少,处理速度快的优点,所以通常音频,视频和普通数据在传送时,使用UDP较多,因为即使丢失少量的包,也不会对接受结果产生较大的影响. 传输层无法保证数据的可靠传输 ...