Spring___Aware接口

博客介绍了Spring的Aware接口,它是标识接口,有一系列以Aware结尾的子接口,这些子接口定义了set方法用于感知Spring Bean相关内容。还通过案例说明实现Aware系列接口的bean会与Spring容器绑定,得以访问容器,给出了Pojo类、配置文件和测试类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

在spring中bean的创建完成后,它无法直接感知到自己的很多信息,如类加载器,BeanFactory,类名等..它需要借助其他来在spring中获取感知到这些信息
Aware系列的接口的主要作用是辅助bean来访问Spring 容器的

如果实现了Aware相关的结果,那么相关的set方法会在初始化之前执行

(已感知的,意识到的,捕获感知)
位于org.springframework.beans.factory包下;
实现了Aware系列子接口的bean是可以访问到Spring容器,而且实现了Aware系列子接口的bean会得到增强,实现了Aware系列接口表明:可以意识到、可以察觉到,但是由于与Spring进行了绑定所以与Spring的耦合度增大。

在这里插入图片描述

二、
Aware接口是个标识接口,它里面没有方法,但它有一系列的子接口皆以Aware为结尾的命名

这些子接口的命名都是以Aware结尾的,从名字上看就可以知道名字Aware前面的就是它的参数类型,Aware系列的子接口里都定义了一个set方法;用于感知Spring Bean的相关内容。

在这里插入图片描述


三、
通过案例来说明,实现了Aware的系列接口的bean会和spring容器进行绑定,使得bean得以访问spring容器。
Pojo类,这里实现了ApplicationContextAware、BeanClassLoaderAware、BeanFactoryAware、BeanNameAware这些Aware系列子接口。

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Dog implements ApplicationContextAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware {
	private String name; 
	private String color;
	
	// 从spring容器中感知到bean的name
	private String beanName; 
	// 从spring容器中感知到这个bean在BeanFactory中的位置
	private BeanFactory beanFactory; 
	// 从spring容器中感知到这个bean在ApplicationContext中的位置
	private ApplicationContext app; 
	// 从spring容器中感知到这个bean的类加载器
	private ClassLoader classLoader;
	
	/**
	 * 
	 */
	@Override
	public void setBeanName(String name) {
		// TODO Auto-generated method stub
		System.out.println("----setBeanName----");
		this.beanName = name;
	}
	/**
	 * 
	 */
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("----setBeanFactory----");
		this.beanFactory =  beanFactory;
	}
	/**
	 * 
	 * 
	 */
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("----setBeanClassLoader----");
		// TODO Auto-generated method stub
		this.classLoader = classLoader;
	}
	/**
	 * 
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("----setApplicationContext----");
		this.app = applicationContext;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}
	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}
	/**
	 * @return the app
	 */
	public ApplicationContext getApp() {
		return app;
	}
	/**
	 * @param app the app to set
	 */
	public void setApp(ApplicationContext app) {
		this.app = app;
	}
	/**
	 * @return the classLoader
	 */
	public ClassLoader getClassLoader() {
		return classLoader;
	}
	/**
	 * @param classLoader the classLoader to set
	 */
	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}
	/**
	 * @return the beanName
	 */
	public String getBeanName() {
		return beanName;
	}
	/**
	 * @return the beanFactory
	 */
	public BeanFactory getBeanFactory() {
		return beanFactory;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Dog [name=" + name + ", color=" + color + ", beanName=" + beanName + ", beanFactory=" + beanFactory
				+ ", app=" + app + ", classLoader=" + classLoader + "]";
	}
	public Dog(String name, String color, String beanName, BeanFactory beanFactory, ApplicationContext app,
			ClassLoader classLoader) {
		super();
		this.name = name;
		this.color = color;
		this.beanName = beanName;
		this.beanFactory = beanFactory;
		this.app = app;
		this.classLoader = classLoader;
	}
	public Dog() {
		super();
		System.out.println("无参构造实例化...");
		// TODO Auto-generated constructor stub
	}
	
	/**
	 *bean里的init-method自定义初始化
	 */
	public void init() {
		// TODO Auto-generated method stub
		System.out.println("bean里的init-method自定义初始化init()");
	}
}

applicationContext.xml配置:

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<bean class="com.pojo.Dog" id="dog" init-method="init">
			<property name="name" value="小狗"/>
			<property name="color" value="黑色"></property>
		</bean>
</beans>

测试类:

public class Test {
	@SuppressWarnings("resource")//去警告,问题不大不要慌
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		Dog bean = (Dog) applicationContext.getBean("dog");
		System.out.println("beanFactory:"+bean.getBeanFactory()); 
		System.out.println("beanName:"+bean.getBeanName()); 
		System.out.println("applicationContext:"+bean.getApp());
		System.out.println("classLoader:"+bean.getClassLoader());
		System.out.println(bean);
	}
} 

结果:

这里把Dog这个bean交给了spring容器管理,实现Aware系列接口的pojo类,可以看到bean与spring进行了绑定,并且可以访问到spring.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值