Spring Boot实战笔记(五)-- Spring高级话题(Spring Aware)

一、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   应用事件发布器,可以发布事件,前面提到的 DemoPulisher 也可实现这个接口来发布事件
  • ResourceLoaderAware            获得资源加载器,可以获得外部资源文件

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

示例:

  1.准备。在aware文件下新建一个test.txt,内容随意,给下面的外部资源加载使用

  2.Spring Aware演示Bean

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

import java.io.IOException;

@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{ // 实现BeanNameAware和ResourceLoaderAware接口,获得Bean名称和资源加载的服务

    private String beanName;

    private ResourceLoader loader;

    @Override
    public void setBeanName(String beanName) { // 实现BeanNameAware需重写setBeanName方法
        this.beanName = beanName;
    }

    @Override
    public void setResourceLoader(ResourceLoader loader) { // 实现ResourceLoaderAware需重写setResourceLoader方法
        this.loader = loader;
    }

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

        Resource resource = loader.getResource("classpath:com/ecworking/aware/test.txt");

        try {
            System.out.println("ResourceLoader加载的文件内容为:" + IOUtils.toString(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  3.配置类

package com.ecworking.aware;

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

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

  4.运行

package com.ecworking.aware;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);

        AwareService service = context.getBean(AwareService.class);

        service.outputResult();

        context.close();
    }
}

运行结果:

原文地址:https://www.cnblogs.com/dyppp/p/7723960.html