Spring Aware




文章目录

  • 1.概述
  • 2.建立包和相关文件
  • 3.定义用于演示的Bean
  • 4.定义配置类
  • 5.定义测试主类
  • 6.测试






1.概述

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

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

Spring提供的Aware接口如下表所示。

spring boot 接口命名 springboot aware接口_Spring Aware


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

下面我们通过一个实际例子来实用一下上表中的BeanNameAware和ResourceLoaderAware接口





2.建立包和相关文件

首先我们在 “src/main/java/com/study/spring/” 下建立一个新的包"ch3.aware",然后在这个包下再新建3个类;同时,在resources下也建立同样的文件结构"com/study/spring/ch3/aware",并在其中建立一个测试文件"test.txt",内容随意,我写的内容为:

this is a test file for aware
and this is the second row

整个项目的结构如下图所示:

spring boot 接口命名 springboot aware接口_Spring Aware_02




3.定义用于演示的Bean

类AwareService的内容如下:

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

import java.io.IOException;

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

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

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

    public void outputResult(){
        System.out.println("Bean的名称为:"+beanName);
        Resource resource = loader.getResource("com/study/spring/ch3/aware/test.txt");
        try{
            System.out.println("ResourceLoader加载的文件内容为:"+ IOUtils.toString(resource.getInputStream()));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}





4.定义配置类

配置类AwareConfig的内容如下:

package com.study.spring.ch3.aware;

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

@Configuration
@ComponentScan("com.study.spring.ch3.aware")
public class AwareConfig {
}





5.定义测试主类

测试主类Main的内容如下:

package com.study.spring.ch3.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();
    }
}





6.测试

选择刚刚建立的测试主类Main文件,右键,在弹出的窗体中选择"Run",效果如下:

spring boot 接口命名 springboot aware接口_spring boot 接口命名_03


结果表明,通过实现BeanNameAware和ResourceLoaderAware接口能够分别获取到当前容器中的Bean名称和取得外部资源文件。