Spring Aware

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

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


那spring为我们提供了哪些Aware接口呢》

BeanNameAware  获得容器中Bean的名称
BeanFactoryAware 获得当前的bean factory,这样就可以调用容器的服务
ApplicationContextAware* 获得当前的application context,这样就可以调用容器的服务
MessageSourceAware 获得message source,这样就可以会的文本信息
ApplicationEventPublisherAware 应用事件发布器,可以发布事件
ResourceLoaderAware 获得资源加载器,可以获得外部资源文件

Spring Aware的目的就是为了Bean获得Spring容器的服务。因为ApplicationContext接口集成了MessageSource接口,ApplicationEventPublisher接口和ResourceLoader接口,所以Bean集成了ApplicationContextAware接口就可以获得Spring容器的所有服务,但原则上我们还是用到什么接口,就实现什么接口。


示例如下:

(1)在lwh.highlight_spring4.ch3.aware下创建一个test.txt,内容随意,给下面的外部资源加载使用

(2)Spring Aware演示Bean

package com.lwh.highlight_spring4.ch3.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 luwenhu on 2017/9/20.
 */
//实现了BeanNameAware,ResourceLoaderAware接口,获得了Bean名称和加载资源的服务
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{

    private String beanName;
    private ResourceLoader loader;

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

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

    public void outputResult(){
        System.out.println("Bean的名称是:"+beanName);
        Resource resource = loader.getResource("classpath:com/lwh/highlight_spring4/ch3/aware/test.txt");
        try{
            String string = IOUtils.toString(resource.getInputStream());
            System.out.println("ResourceLoader加载的内容为:"+string);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

(3)配置类

package com.lwh.highlight_spring4.ch3.aware;

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

/**
 * Created by luwenhu on 2017/9/20.
 */
@Configuration
@ComponentScan("com.lwh.highlight_spring4.ch3.awar")
public class AwareConfig {
}

(4)运行类

package com.lwh.highlight_spring4.ch3.aware;

import com.lwh.highlight_spring4.ch1.aop.AopConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by luwenhu on 2017/9/20.
 */
public class Main {
    public  static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareService.class);
        AwareService awareService = context.getBean(AwareService.class);
        awareService.outputResult();
        context.close();
    }
}

(5)运行结果

原文地址:https://www.cnblogs.com/wenhulu/p/7560806.html