一、流程图
二、各环节介绍
- spring管理bean,通过反射找到构造方法,实例化bean对象
- Bean实例化后对将Bean的引入和值注入到Bean的属性中
- BeanNameAware.setBeanName(),获取设置bean的名称
- BeanFactoryAware.setBeanFactory(),获取BeanFactory容器实例即获取bean
- ApplicationContextAware.setApplicationContext(),获取bean所在应用上下文引用
- BeanPostProcessor.postProcessBeforeInitialization(),前置处理器,获取bean,为bean填充属性
- InitializingBean.afterPropertiesSet(),初始化方法
- bean使用init-method声明了初始化方法
- BeanPostProcessor.postProcessBeforeInitialization(),后置处理器,获取bean
- DisposableBean.destory(),销毁方法
- bean使用destory-method声明了销毁方法
三、代码测试
public class Book implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
InitializingBean, DisposableBean {
private String bookName;
public void setBookName(String bookName) {
System.out.println("book注入属性值");
this.bookName = bookName;
}
public Book() {
System.out.println("book对象实例化");
}
@Override
public void setBeanName(String name) {
System.out.println("获取bean名字");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("获取bean工厂实例");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("获取应用上下文");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("初始化方法");
}
public void definedInit(){
System.out.println("自定义初始化方法");
}
@Override
public void destroy() throws Exception {
System.out.println("销毁方法");
}
public void definedDestroy(){
System.out.println("自定义销毁方法");
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
// 容器加载的时候会加载一些其他的bean,会调用初始化前和初始化后方法
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Book){
System.out.println("BeanPostProcessor.before invoke");
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Book){
System.out.println("BeanPostProcessor.after invoke");
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="book" class="com.xc.com.xc.entity.Book" init-method="definedInit" destroy-method="definedDestroy" >
<property name="bookName" value="西游记"/>
</bean>
<bean id="myBeanPostProcessor" class="com.xc.com.xc.entity.MyBeanPostProcessor" />
</beans>
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = (Book) context.getBean("book");
context.destroy();
}
}
结果:
book对象实例化
book注入属性值
获取bean名字
获取bean工厂实例
获取应用上下文
BeanPostProcessor.before invoke
初始化方法
自定义初始化方法
BeanPostProcessor.after invoke
销毁方法
自定义销毁方法
https://www.cnblogs.com/javazhiyin/p/10905294.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/148675.html