Spring中Aware的用法以及实现

Aware

在Spring当中有一些内置的对象是未开放给我们使用的,例如Spring的上下文ApplicationContext、环境属性Environment,BeanFactory等等其他的一些内置对象,而在我们可以通过实现对应的Aware接口去拿到我们想要的一些属性,一般命名都是xxxAware,在创建对象的时候, 会调用接口规定的方法注入到相关组件:Aware

常用的Aware:

用法示例:

源码解析

我们先了解什么是BeanPostProcessor,是在创建Bean之前以及创建Bean之后的一种后置处理器,这里就简单的讲一下这个,就是在我们使用的这些注解,例如:@Async (异步执行),@Bean,@Component、@Autowired、@PropertySource等等都需要经过BeanPostProcessor去处理才能加载到容器当中,

先看一个示例:

不仅仅有BeanPostProcessor还有BeanDefinitionRegistryPostProcessor(Bean注册器后置处 理器)、BeanFactoryPostProcessor(BeanFactory后置处理器),

在处理Aware实现类的时候就会有对应的一个AwareProcessor去处理,我们拿ApplicationContextAware做示例**:**

我们找到对应的后置处理器ApplicationContextAwareProcessor

因为BeanPostProcessor有两个接口实现,但是只对处理前做内置对象赋值,将对应的对象暴露给我们

复制代码
class ApplicationContextAwareProcessor implements BeanPostProcessor {

	private final ConfigurableApplicationContext applicationContext;

	private final StringValueResolver embeddedValueResolver;


	/**
	 * Create a new ApplicationContextAwareProcessor for the given context.
	 */
	public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
		this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
	}


	@Override
	@Nullable
	public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
		AccessControlContext acc = null;

		if (System.getSecurityManager() != null &&
				(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
						bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
						bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
			acc = this.applicationContext.getBeanFactory().getAccessControlContext();
		}

		if (acc != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				invokeAwareInterfaces(bean);
				return null;
			}, acc);
		}
		else {
			invokeAwareInterfaces(bean);
		}

		return bean;
	}

	private void invokeAwareInterfaces(Object bean) {
		if (bean instanceof Aware) {
			if (bean instanceof EnvironmentAware) {
				((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
			}
			if (bean instanceof EmbeddedValueResolverAware) {
				((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
			}
			if (bean instanceof ResourceLoaderAware) {
				((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
			}
			if (bean instanceof ApplicationEventPublisherAware) {
				((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
			}
			if (bean instanceof MessageSourceAware) {
				((MessageSourceAware) bean).setMessageSource(this.applicationContext);
			}
			if (bean instanceof ApplicationContextAware) {
				((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
			}
		}
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		return bean;
	}

}

不得不说Spring的这种设计还是很厉害的,建议可以买本Spring IOC源码分析的书看看比较好,了解的更加细致

相关推荐
程序员清风13 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
李广坤13 小时前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
皮皮林55114 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
NE_STOP17 小时前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
华仔啊19 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing19 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠1 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家2 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java