Spring中的Aware接口

Aware接口

Aware接口从字面上翻译过来是感知捕获的含义。(Aware是”意识到的,察觉到的“意思,可得结论实现了Aware系列接口表明可以意识到,可以察觉到)

单纯的bean(指没有实现Aware接口的bean对象)是没有”知觉“的,实现了Aware系列接口的bean可以访问Spring容器,这些Aware系列接口增强了Spring bean的功能,但是也会造成对Spring框架的绑定,增大了与Spring框架的耦合度。

接口源码如下:

// 该接口没有定义任何方法,只是一个标识接口
public interface Aware {
    
}

Aware的常用子接口有如下:

  1. org.springframework.context.ApplicationContextAware

    实现该接口的类可以获取spring容器上下文信息 ApplicationContext。

  2. org.springframework.beans.factory.BeanFactoryAware

    根据bean名称获取相应的bean对象。

  3. org.springframework.beans.factory.BeanClassLoaderAware

    获取Spring容器的类加载器ClassLoader对象。

  4. org.springframework.beans.factory.BeanNameAware

    这个接口的含义就是让实现类知道自己在spring容器中定义的beanName是什么,实际一般开发没啥用。

  5. org.springframework.context.EnvironmentAware

    实现这个接口的类能够获取Environment对象,进而可以获取各种系统变量信息,也可以设置变量的有限级别等等;通过Environment对象可以获取Spring boot框架中的application.properties中定义的变量值。

  6. org.springframework.context.ResourceLoaderAware

    获取资源加载器ResourceLoader对象,可以获得外部资源文件。

  7. org.springframework.context.annotation.ImportAware

Aware系列接口中的部分常用接口:

public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
public interface BeanClassLoaderAware extends Aware {
    void setBeanClassLoader(ClassLoader classLoader);
}
public interface BeanFactoryAware extends Aware {
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}

可以发现Aware系列接口的共性:

  1. 都是以Aware单词进行结尾
  2. 都是Aware接口的子接口,即都实现了Aware接口
  3. 接口内均定义了一个set方法,而方法中的参数是该系列接口名称Aware单词前面的内容,也就是当前bean需要感知的内容,所以我们需要在bean中声明相关的成员变量来接收

举例说明:

@Component
public class ApplicationContextStaticProvider implements ApplicationContextAware {

	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		return context;
	}

	// 通过beanName获取Bean
	public static Object getBean(String beanName) {
		return getApplicationContext().getBean(beanName);
	}

	
	// 通过class获取bean
	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}

	// 通过name,以及Clazz返回指定的bean
	public static <T> T getBean(String beanName, Class<T> clazz) {
		return getApplicationContext().getBean(beanName, clazz);
	}
}

@Service
public class DemoBean {
	public void test(String str){
		System.out.println(str);
	}
}

public class AnnotationConfigApplicationContextTests {
	@Test
	public void scanAndRefreshTestAware() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.scan("org.springframework.context.annotation7");
		context.refresh();
		DemoBean demoBean= (DemoBean) ApplicationContextStaticProvider.getBean("demoBean");
		demoBean.test("test applicationContextAware");
	}
}

来源:

  1. Spring之Aware接口介绍
  2. spring之Aware接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值