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

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

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

查看所有标签

猜你喜欢:

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

淘宝、天猫网上开店速查速用一本通

淘宝、天猫网上开店速查速用一本通

刘珂 / 北京时代华文书局 / 2015-6 / 39.8

为了帮助众多的新手卖家掌握淘宝天猫网上开店、货源准备、店铺装修、商品拍摄、交易方法、营销推广以及售后服务等知识,本书作者根据自己多年网上开店心得,并结合了多名淘宝五皇冠店主和天猫旗舰店卖家的经验,精心策划编写了本书。 《淘宝、天猫网上开店速查速用一本通:开店、装修、运营、推广完全攻略》将目前最前沿、最流行的营销理念运用到淘宝天猫网上平台,所有技术都在实际应用获得显著效果,并且还在持续创造着惊......一起来看看 《淘宝、天猫网上开店速查速用一本通》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

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

正则表达式在线测试