Spring高级话题-Aware

一、Aware是什么?

Spring Aware就是一些定义了Spring容器本身功能资源的接口

Spring提供的Aware接口
接口备注
BeanNameAware获得到容器中Bean的名称
BeanFactoryAware获得当前 bean factory,这样可以调用容器的服务
ApplicationContextaware*当前的 application context,这样可以调用容器的服务
MessageSourceAware获得 message source,这样可以获得文本信息
ApplicationEventPublisherAware应用事件发布器,可以发布事件
ResourceLoaderAware获得资源加载器,可以获得外部资源文件
二、什么时候用Aware

当某一个Bean需要获得Spring容器的服务时,可以实现对应的Aware接口。
注意: 这样会造成Bean与Spring框架的耦合性增加

三、AwareDemo

bean

package com.cactus.demo.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;

/**
 * Created by liruigao
 * Date: 2019-12-05 14:35
 * Description:
 */

@Service
public class AwareDemo implements BeanNameAware, ResourceLoaderAware {
    private String beanName;
    private ResourceLoader resourceLoader;
    public void setBeanName(String s) {
        this.beanName = s;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void show() {
        System.out.println("beanName : " + beanName);
        Resource resource = resourceLoader.getResource("classpath:awaredemo/awaredemo.txt");
        String desc = resource.getDescription();
        System.out.println("resource desc : " + desc);
        try {
            String streamStr = IOUtils.toString(resource.getInputStream(), "utf-8");
            System.out.println("resource streamStr : " + streamStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

配置类

package com.cactus.demo.aware;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by liruigao
 * Date: 2019-12-05 14:43
 * Description:
 */

@Configuration
@ComponentScan("com.cactus.demo.aware")
public class AwareConfig {
}

awaredemo.txt

this is a awaredemo!

Main

package com.cactus.demo.aware;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by liruigao
 * Date: 2019-12-05 14:45
 * Description:
 */


public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
        AwareDemo awareDemo = context.getBean(AwareDemo.class);
        awareDemo.show();
        context.close();
    }
}

Result

beanName : awareDemo
resource desc : class path resource [awaredemo/awaredemo.txt]
resource streamStr : this is a awaredemo!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜鹊先生Richard

随缘~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值