Example of BeanFactoryAware in Spring--转
Spring -- in of Example
2023-09-11 14:21:40 时间
原文地址:http://www.concretepage.com/spring/example_beanfactoryaware_spring
If a bean in spring implements BeanFactoryAware then that bean has to implement a method that issetBeanFactory. And when that bean is loaded in spring container, setBeanFactory is called. BeanFactoryAware belongs to the package org.springframework.beans.factory. BeanFactoryAware awares the bean for its BeanFactory.
A.java
package com.concretepage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class A implements BeanFactoryAware{
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("setBeanFactory:"+beanFactory);
}
public A(){
System.out.println("Bean A is Initialized.");
}
}
app-conf.xml
<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-3.0.xsd ">
<bean id="testA" class="com.concretepage.A"/>
</beans>
SpringTest.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml");
context.registerShutdownHook();
}
}
Output
Bean A is Initialized.
setBeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@16df1832: defining beans [testA]; root of factory hierarchy
相关文章
- 8 -- 深入使用Spring -- 6...1 Spring支持的事务策略
- 7 -- Spring的基本用法 -- 8... 抽象Bean与子Bean;Bean继承与Java继承的区别;容器中的工厂Bean;获得Bean本身的id;强制初始化Bean
- Spring源码之Spring后置处理器详解
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
- HikariCP和spring-boot-devtools了解
- Spring Boot JPA的查询语句
- spring MVC、mybatis配置读写分离
- 【spring mvc】Spring MVC 的参数解析器ArgumentResolver阐述
- spring boot打包成war包的页面该放到哪里?
- 你的响应阻塞了没有?--Spring-WebFlux源码分析
- How To Do @Async in Spring--转
- CORS with Spring MVC--转
- CORS support in Spring Framework--官方
- 6-tips-for-managing-property-files-with-spring--转
- Example of ApplicationContextAware in Spring--转
- Spring – ${} is not working in @Value--转载
- spring beans源码解读之--BeanFactory的注册