Aware接口作用

介绍

Aware(感知)接口是一个标记,里面没有任何方法,实际方法定义都是子接口确定(相当于定义了一套规则,并建议子接口中应该只有一个无返回值的方法)。

我们知道spring已经定义好了很多对象,如ApplicationContext、BeanFactory、Environment等,但是这些对象是spring框架自身的,我们去获取这些是及其困难的,所以spring定义了一套规则能让我们很容易得获取框架中的对象,这就是Aware的意义,现在对Aware有一定了解了吧,Aware是感知spring容器中的对象。

spring定义了很多子接口并已实现可直接可用,下图均为spring中的子接口。
在这里插入图片描述
如图第一个ApplicationContextAware,类名很清晰的告诉我们是感知ApplicationContext,ApplicationContext都知道是spring容器,所以这里表示感知容器,就是我们想获取ApplicationContext只需要实现这个接口就行。

注意
这里的接口名都是有规则的,如ApplicationContextAware就是获取ApplicationContext的,BeanFactoryAware就是获取BeanFactory的,见名知意。

源码

public interface ApplicationContextAware extends Aware {

	/**
	 * 只有一个方法,实现这方法spring在启动过程中会默认将
	 * 调用此方法并将ApplicationContext参数传递过来,这样
	 * 我们就能很容易的获取到ApplicationContext了
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

例子

Teacher类并实现ApplicationContextAware 接口

package com.lp.entity;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Teacher implements ApplicationContextAware {
	private String name;

	private ApplicationContext applicationContext;

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher{" +
				"name='" + name + '\'' +
				'}';
	}

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

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="com.lp"/>

	<bean id="teacher" class="com.lp.entity.Teacher">
		<property name="name" value="zhangsan"/>
	</bean>
</beans>

测试,看一看Teacher类中能不能获取到ApplicationContext对象

package example.lp;

import com.lp.entity.Teacher;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		Teacher teacher = (Teacher)context.getBean("teacher");
		System.out.println("------------ApplicationContext="+teacher.getApplicationContext());
	}
}

在这里插入图片描述
从结果中可以清晰的看到已经获取到了ApplicationContext对象,当我们需要其他对象也可以看看其他Aware子接口,直接实现即可。

自定义Aware接口(简单看一下)

spring中有一个ApplicationContextAwareProcessor类,里面就是实现这些子接口功能的,invokeAwareInterfaces方法就是具体逻辑。

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(Object bean, String beanName) throws BeansException {
		if (bean instanceof Aware) {
			invokeAwareInterfaces(bean);
		}
		return bean;
	}

	private void invokeAwareInterfaces(Object bean) {
		if (bean instanceof EnvironmentAware environmentAware) {
			environmentAware.setEnvironment(this.applicationContext.getEnvironment());
		}
		if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
			embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
		}
		if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
			resourceLoaderAware.setResourceLoader(this.applicationContext);
		}
		if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {
			applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);
		}
		if (bean instanceof MessageSourceAware messageSourceAware) {
			messageSourceAware.setMessageSource(this.applicationContext);
		}
		if (bean instanceof ApplicationStartupAware applicationStartupAware) {
			applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());
		}
		if (bean instanceof ApplicationContextAware applicationContextAware) {
			applicationContextAware.setApplicationContext(this.applicationContext);
		}
	}

}

自己实现Aware接口

这个需要了解一下spring启动过程,跟BeanPostProcessor这个接口有关,也需要了解BeanPostProcessor的执行时机,这里我就不介绍了,我自己写了一个示例,这里只是展示怎么实现Aware的。

package com.lp.entity;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Teacher implements ApplicationContextAware {
	private String name;

	private ApplicationContext applicationContext;

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher{" +
				"name='" + name + '\'' +
				'}';
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext =applicationContext;
	}
}
package com.lp.entity;

import com.lp.TeacherAware;

public class Student implements TeacherAware {
	private String name;

	private Teacher teacher;

	public Teacher getTeacher() {
		return teacher;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setTeacher(Teacher teacher) {
		this.teacher =teacher;
	}
}

package com.lp;

import com.lp.entity.Teacher;
import org.springframework.beans.factory.Aware;

public interface TeacherAware extends Aware {
	void setTeacher(Teacher teacher);
}

package com.lp;

import com.lp.entity.Teacher;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class TeacherAwareBeanPostProcessor implements BeanPostProcessor {
	private final ConfigurableApplicationContext applicationContext;

	@Autowired
	public TeacherAwareBeanPostProcessor(ConfigurableApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof TeacherAware teacherAware){
			teacherAware.setTeacher(applicationContext.getBean(Teacher.class));
		}
		return bean;
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="com.lp"/>

	<bean id="teacher" class="com.lp.entity.Teacher">
		<property name="name" value="zhangsan"/>
	</bean>
	<bean id="student" class="com.lp.entity.Student">
		<property name="name" value="lisi"/>
	</bean>

</beans>
package example.lp;

import com.lp.entity.Student;
import com.lp.entity.Teacher;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		//Teacher teacher = (Teacher)context.getBean("teacher");
		//System.out.println("------------ApplicationContext="+teacher.getApplicationContext());

		Student student = (Student)context.getBean("student");
		System.out.println("------------Teacher="+student.getTeacher());
	}
}

从上述代码和结果中可以看到,我创建了两个bean,一个Student和一个Teacher,我用Aware方式实现了将Student中的Teacher属性注入,当然这个场景可能不是很合适,但这里主要是演示怎么自己实现Aware接口,需要自己写一个接口实现Aware接口并之定义一个方法(接口名最好是遵循spring规则,且只有一个void方法),创建一个实现BeanPostProcessor的对象并写出具体实现逻辑,这儿需要你了解spring启动流程并且知道BeanPostProcessor接口的作用。

希望对你有帮助,别忘了点赞!!

  • 31
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值