这篇文章主要介绍了Spring5源码之BeanFactoryAware的接口,在bean初始化之后,可以取得一些资源并对bean进行相关的设置,需要的朋友可以参考一下。
1、BeanFactoryAware接口
/**
* Interface to be implemented by beans that wish to be aware of their
* owning {@link BeanFactory}.
*
* <p>For example, beans can look up collaborating beans via the factory
* (Dependency Lookup). Note that most beans will choose to receive references
* to collaborating beans via corresponding bean properties or constructor
* arguments (Dependency Injection).
*
* <p>For a list of all bean lifecycle methods, see the
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Rod Johnson
* @author Chris Beams
* @since 11.03.2003
* @see BeanNameAware
* @see BeanClassLoaderAware
* @see InitializingBean
* @see org.springframework.context.ApplicationContextAware
*/
public interface BeanFactoryAware extends Aware {
源码定义:接口由希望了解其所属BeanFactory的bean实现。
2、实现BeanFactoryAware接口的案例
- 普通的bean:Hello
package com.test.bean;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Slf4j
public class Hello {
private String text;
public void say() {
log.info("text:{}", text);
}
}
- 实现BeanFactoryAware接口的bean:HelloBeanFactoryAware
package com.test.aware;
import com.test.bean.Hello;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
@Slf4j
public class HelloBeanFactoryAware implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
log.info("beanFactory:{}",beanFactory);
}
public void testAware() {
Hello hello = (Hello) beanFactory.getBean("hello");
hello.setText("Edit Hello World!");
hello.say();
}
}
- resources目录下配置的xml文件:aware.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.test.bean.Hello">
<property name="text" value="Hello World!"></property>
</bean>
<bean id="helloBeanFactoryAware" class="com.test.aware.HelloBeanFactoryAware"></bean>
</beans>
- 测试类:HelloBeanFactoryAwareTest
package com.test;
import com.test.aware.HelloBeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloBeanFactoryAwareTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aware.xml");
HelloBeanFactoryAware helloBeanFactoryAware = (HelloBeanFactoryAware) applicationContext.getBean("helloBeanFactoryAware");
helloBeanFactoryAware.testAware();
}
}
- 程序执行可以看到如下的结果:
18:05:01.317 [main] INFO com.test.aware.HelloBeanFactoryAware - beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@3551a94: defining beans [hello,helloBeanFactoryAware]; root of factory hierarchy
18:05:01.324 [main] INFO com.test.bean.Hello - text:Edit Hello World!
- 总结:从程序的执行结果可以看到两点:
1、我们HelloBeanFactoryAware通过实现BeanFactoryAware接口,然后通过beanFactory.getBean(“hello”)获得Hello类的Bean,进而可以操作Hello的text属性进行修改默认的Hello World!,达到我们需要进行相关的设置目的。
2、BeanFactory接口默认被DefaultListableBeanFactory类实现。
3、源码invokeAwareMethods方法实现
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
// bean是BeanFactoryAware的类型
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
如果您觉得有帮助,欢迎点赞哦 ~ ~ 多谢~ ~