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

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

MySQL权威指南

MySQL权威指南

Randy Jay Yarger / 林琪、朱涛江 / 中国电力出版社 / 2003-11-1 / 49.00元

为一种开源数据库,MySQL已经成为最流行的服务器软件包之一。开发人员在其数据库引擎中提供了丰富的特性(只需很少的内存和CPU支持)。 因此,众多Linux和Unix服务器(以及一些Windows服务器)都采用MySQL作为其数据库引擎。由于MySQL作为Web站点后端时速度特别快而且相当方便,所有在目前流行的一个词LAMP(表示Linux、Apache、MySQL和Perl、Python或......一起来看看 《MySQL权威指南》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换