Spring4.x高级话题(一):Spring Aware

栏目: Java · 发布时间: 9年前

内容简介:Spring4.x高级话题(一):Spring Aware

Spring4.x高级话题(一):Spring Aware

一. 点睛

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

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

Spring 提供的 Aware 接口如下表所示:

Spring提供的Aware接口

BeanNameAware 获得到容器中Bean的名称
BeanFactoryAware 获得当前bean factory,这样可以调用容器的服务
ApplicationContextAware* 获得当前application context,这样可以调用容器的服务
MessageSourceAware 获得message source这样可以获得文本信息
ApplicationEventPublisherAware 应用事件发布器,可以发布事件
ResourceLoaderAware 获得资源加载器,可以获得外部资源文件
Spring Aware 的目的是为了让 Bean 获得 Spring 容器的服务。因为 ApplicationContext 接口集成了 MessageSource 接口, ApplicationEventPublisherAware 接口和 ResourceLoaderAware 接口,所以 Bean 继承 ApplicationContextAware 可以获得 Spring

容器的所有服务,但原则上我们还是用到什么接口就实现什么接口。

二. 示例

1. 准备。

org.light4j.sping4.senior.aware 包下新建一个 test.txt ,内容随意,给下面的外部资源加载使用。

2. Spring Aware演示Bean

package org.light4j.sping4.senior.aware;

import java.io.IOException;

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;

@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{//①

    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {//②
        this.loader = resourceLoader;
    }

    @Override
    public void setBeanName(String name) {//③
        this.beanName = name;
    }

    public void outputResult(){
        System.out.println("Bean的名称为:" + beanName);

        Resource resource = 
                loader.getResource("classpath:org/light4j/sping4/senior/aware/test.txt");
        try{

            System.out.println("ResourceLoader加载的文件内容为: " + IOUtils.toString(resource.getInputStream()));

           }catch(IOException e){
            e.printStackTrace();
           }
    }
}

代码解释:

① 实现 BeanNameAware , ResourceLoaderAware 接口,获得 Bean 名称和资源加载的服务。

② 实现 ResourceLoaderAware 需要重写 setResourceLoader 方法。

③ 实现 BeanNameAware 需要重写 setBeanName 方法。

3. 配置类

package org.light4j.sping4.senior.aware;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.light4j.sping4.senior.aware")
public class AwareConfig {

}

4. 运行

package org.light4j.sping4.senior.aware;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);

        AwareService awareService = context.getBean(AwareService.class);
        awareService.outputResult();

        context.close();
    }
}

运行结果如下图所示:

Spring4.x高级话题(一):Spring Aware

5. 源代码示例:

github 地址: 点击查看

码云地址: 点击查看

Spring4.x高级话题(一):Spring Aware 欢迎关注人生设计师的微信公众账号

以上所述就是小编给大家介绍的《Spring4.x高级话题(一):Spring Aware》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

你必须知道的213个C语言问题

你必须知道的213个C语言问题

范立锋、李世欣 / 人民邮电出版社 / 2010-6 / 45.00元

《你必须知道的213个C语言问题》精选了213个在C语言程序设计中经常遇到的问题,目的是帮助读者解决在C语言学习和开发中遇到的实际困难,提高读者学习和开发的效率。这些问题涵盖了C语言与软件开发、C语言基础、编译预处理、字符串、函数、键盘操作、文件、目录和磁盘、数组、指针和结构、DOS服务和BIOS服务、日期和时间、重定向I/O和进程命令、C语言开发常见错误及程序调试等内容,均是作者经过充分的调研,......一起来看看 《你必须知道的213个C语言问题》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试