Spring Aware (Spring 感知)

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:应用事件发布器,可以发布事件。再DemoPublisher中也可以实现这个接口来发布事件 
  • ResourceLoaderAware: 获得资源加载器,可以获得外部资源
Spring Aware的目的是让Bean获得Spring的容器服务。因为ApplicationContext接口继承了MessageSource接口、ApplicationEventPulisher接口和ResourceLoader接口,所以Bean继承ApplicationContextAware可以获得容器所有服务,但是原则上我们还是用到什么接口,就实现什么接口。

示例:
Spring Aware演示的Bean:
package com.chenfeng.xiaolyuh.aware.service;

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;

/**
 * Spring Aware的目的是让Bean获得Spring的容器服务,主要接口是:
 * BeanNameAware: 获取容器中Bean的名称
 * BeanFactoryAware:获得当前Bean Factory,这样可以调用容器的服务
 * ApplicationContextAware:获得当前Application Context,这样可以获得容器的服务
 * MessageSourceAware:获得Message Source,这样可以获得文本信息
 * ApplicationEventPublisherAware:应用事件发布器,可以发布事件。再DemoPublisher中也可以实现这个接口来发布事件
 * ResourceLoaderAware: 获得资源加载器,可以获得外部资源
 */
@Service
// 实现BeanNameAware、ResourceLoaderAware接口,获得Bean的名称和资源加载服务。
public class DemoAwareService implements BeanNameAware, ResourceLoaderAware {

	private String beanBane;
	
	private ResourceLoader resourceLoader; 
	
	@Override
	// 实现ResourceLoaderAware接口需要重写setResourceLoader方法
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	@Override
	// 实现BeanNameAware接口需要重写setBeanName方法
	public void setBeanName(String beanBane) {
		this.beanBane = beanBane;
	}
	
	@Override
	public String toString() {
		String output = "";
		Resource resource = resourceLoader.getResource("classpath:aware/test.txt");
		try {
			output = "ResourceLoader加载的文件内容为:" + IOUtils.toString(resource.getInputStream());
		} catch (IOException e) {
		}
		return output + " Bean的名字为:" + beanBane;
	}
}


配置类:
package com.chenfeng.xiaolyuh.aware.config;

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

@Configuration // 声明当前类是一个配置类,相当于Spring配置的XML文件
@ComponentScan(basePackages={"com.chenfeng.xiaolyuh.aware"})
public class AwareConfig {
		
}


测试类:
package com.chenfeng.xiaolyuh.test;

import org.junit.After;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.chenfeng.xiaolyuh.aware.config.AwareConfig;
import com.chenfeng.xiaolyuh.aware.service.DemoAwareService;

public class SpringAwareTest {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);

	@Test
	public void contextTest() {
		DemoAwareService demoAwareService = context.getBean(DemoAwareService.class);
		System.out.println(demoAwareService.toString());
	}

	@After
	public void closeContext() {
		context.close();
	}

}


结果:
三月 16, 2017 11:19:19 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Thu Mar 16 11:19:19 CST 2017]; root of context hierarchy
ResourceLoader加载的文件内容为:Hello Spring Aware! Bean的名字为:demoAwareService
三月 16, 2017 11:19:19 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Thu Mar 16 11:19:19 CST 2017]; root of context hierarchy



演示获得Spring容器中的Bean:
 
package com.xiaolyuh.holder;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    }

    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBeansOfType(clazz);
    }

    /**
     * 清除applicationContext静态变量.
     */
    public static void cleanApplicationContext() {
        applicationContext = null;
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }
}

测试类
    @Test
    public void testApplicationContextAware() {
        RedisTemplate redisTemplate = SpringContextHolder.getBean("redisTemplate");
        System.out.println(redisTemplate);
    }


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值