Aware接口文章
- 一、使用Aware与不使用Aware的效果
- 二、ApplicationContext实例介绍Aware
- 三、从Aware顶级接口实现自己的XXXAware
一、使用Aware与不使用Aware的效果
参考文章:Spring中的aware接口
- 定义BeanNameAware接口
public interface BeanNameAware extends Aware {
void setBeanName(String name);
}
- 定义两个User,一个实现BeanNameAware,一个不实现BeanNameAware
public class User implements BeanNameAware{
private String id;
private String name;
private String address;
public void setBeanName(String beanName) {
//ID保存BeanName的值
id=beanName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
public class User2 {
private String id;
private String name;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
- 在Spring配置文件中初始化两个对象
<bean id="zhangsan" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User">
<property name="name" value="zhangsan"></property>
<property name="address" value="火星"></property>
</bean>
<bean id="lisi" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User2">
<property name="name" value="lisi"></property>
<property name="address" value="火星"></property>
</bean>
- main方法测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-beanaware.xml");
User user=context.getBean(User.class);
System.out.println(String.format("实现了BeanNameAware接口的信息BeanId=%s,所有信息=%s",user.getId(),user.toString()));
User2 user2=context.getBean(User2.class);
System.out.println(String.format("未实现BeanNameAware接口的信息BeanId=%s,所有信息=%s",user2.getId(),user2.toString()));
}
- 运行结果
实现了BeanNameAware接口的信息BeanId=zhangsan,所有信息=User{id='zhangsan', name='zhangsan', address='火星'}
未实现BeanNameAware接口的信息BeanId=null,所有信息=User{id='null', name='lisi', address='火星'}
- 分析结果
- 能够看到,我们在实现了BeanNameAware的 User中,获取到了Spring容器中的BeanId(对应spring配置文件中的id属性),而没有实现BeanNameAware的User2,则不能获取到Spring容器中的Id属性。
- 所以BeanNameAware接口是为了让自身Bean能够感知到,获取到自身在Spring容器中的id属性。
- 同理,其他的Aware接口也是为了能够感知到自身的一些属性。
比如实现了ApplicationContextAware接口的类,能够获取到ApplicationContext,实现了BeanFactoryAware接口的类,能够获取到BeanFactory对象。
二、ApplicationContext实例介绍Aware
参考文章:Spring的Aware接口原来是这么回事
- 想要使用ApplicationContext对象,就要实现ApplicationContextAware接口
public class MyApplicationContextAware implements ApplicationContextAware {
// 用一个全局变量,保存接口回调时传入的applicationContext对象
private ApplicationContext applicationContext;
// 使用获取的applicationContext对象,查看容器中有哪些bean.
public void printAllBean(){
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
System.out.println("当前容器中的bean如下:");
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
- 把MyApplicationContextAware这个bean注册到容器中
<bean id="myApplicationContextAware"
class="com.kaka.spring.context.aware.MyApplicationContextAware"/>
- 执行代码
@Test
public void awareTest(){
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
MyApplicationContextAware myApplicationContextAware =
classPathXmlApplicationContext.getBean("myApplicationContextAware",MyApplicationContextAware.class);
myApplicationContextAware.printAllBean();
}
- 运行结果
三、从Aware顶级接口实现自己的XXXAware
参考视频:Aware依赖注入管理