赞
踩
Spring bean的生命周期
A. 5步骤版本
1.实例化
2.依赖注入
3.初始化
4.使用
5.销毁
A.1 代码示例
#创建一个对象,自定义销毁以及初始化方法 package com.ruoyi.bizsys.domain; public class AccountInfo { private String acctNo; public AccountInfo(String acctNo) { this.acctNo = acctNo; } public AccountInfo() { System.out.println("1.实例化"); } public String getAcctNo() { return acctNo; } public void setAcctNo(String acctNo) { System.out.println("2.依赖注入"); } public void myInit(){ System.out.println("3.初始化"); } public void myDestory(){ System.out.println("5.销毁"); } }
<!-- 在配置文件中配置初始化方法 销毁方法 以及属性-->
<?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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
<property name="acctNo" value="123456789"></property>
</bean>
</beans>
public class MyTest {
//测试类测试5个步骤
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
Object o = context.getBean(AccountInfo.class);
System.out.println("4.使用");
((ClassPathXmlApplicationContext) context).close();
}
}
A.2 输出结果
B. 7步骤版本
1.实例化
2.依赖注入
3.初始化前 BeanPostProcessor before方法 ⇒ postProcessBeforeInitialization(Object obj, String s)
4.初始化
5.初始化结束时 BeanPostProcessor after方法 ⇒ postProcessAfterInitialization(Object obj, String s)
6.使用
7.销毁
B. 1 代码示例
package com.ruoyi.bizsys.domain; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; @Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AccountInfo){ System.out.println("3.初始化前 BeanPostProcessor before方法 "); } return bean; } /** * 同上,不过时在初始化之后执行 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AccountInfo){ System.out.println("5.初始化后 BeanPostProcessor after 方法 "); } 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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
<property name="acctNo" value="123456789"></property>
</bean>
<bean id="MyBeanPostProcessor" class="com.ruoyi.bizsys.domain.MyBeanPostProcessor">
</bean>
</beans>
package com.ruoyi.bizsys.domain; public class AccountInfo { private String acctNo; public AccountInfo(String acctNo) { this.acctNo = acctNo; } public AccountInfo() { System.out.println("1.实例化"); } public String getAcctNo() { return acctNo; } public void setAcctNo(String acctNo) { System.out.println("2.依赖注入"); } public void myInit(){ System.out.println("4.初始化"); } public void myDestory(){ System.out.println("7.销毁"); } }
package com.ruoyi.bizsys.test; import com.ruoyi.bizsys.domain.AccountInfo; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml"); Object o = context.getBean(AccountInfo.class); System.out.println("4.使用"); ((ClassPathXmlApplicationContext) context).close(); } @Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml"); Object o = context.getBean(AccountInfo.class); System.out.println("6.使用"); ((ClassPathXmlApplicationContext) context).close(); } }
B. 2 输出结果
C. 10步骤版本
1.实例化
2.依赖注入
3.BeanNameAware,BeanFactoryAware,ApplicationContextAware方法执行啦
【BeanNameAware#setBeanName,BeanFactoryAware#setBeanFactory,ApplicationContextAware#setApplicationContext】
4.初始化前 BeanPostProcessor before方法 ⇒ postProcessBeforeInitialization(Object obj, String s)
5.InitializingBean 方法执行啦
6.初始化
7.初始化结束时 BeanPostProcessor after方法 ⇒ postProcessAfterInitialization(Object obj, String s)
8.使用
9.DisposableBean 方法执行啦
10.销毁
C. 1 代码示例
package com.ruoyi.bizsys.domain; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; public class AccountInfo implements BeanFactoryAware, InitializingBean, DisposableBean { private String acctNo; public AccountInfo(String acctNo) { this.acctNo = acctNo; } public AccountInfo() { System.out.println("1.实例化"); } public String getAcctNo() { return acctNo; } public void setAcctNo(String acctNo) { System.out.println("2.依赖注入"); } public void myInit(){ System.out.println("6.初始化"); } public void myDestory(){ System.out.println("10.销毁"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("3.BeanNameAware,BeanFactoryAware,ApplicationContextAware方法执行啦" ); } @Override public void destroy() throws Exception { System.out.println("9.DisposableBean 方法执行啦"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("5.InitializingBean 方法执行啦" ); } }
<?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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
<property name="acctNo" value="123456789"></property>
</bean>
<bean id="MyBeanPostProcessor" class="com.ruoyi.bizsys.domain.MyBeanPostProcessor">
</bean>
</beans>
package com.ruoyi.bizsys.domain; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; @Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AccountInfo){ System.out.println("4.初始化前 BeanPostProcessor before方法 "); } return bean; } /** * 同上,不过时在初始化之后执行 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AccountInfo){ System.out.println("7.初始化后 BeanPostProcessor after 方法 "); } return bean; } }
package com.ruoyi.bizsys.test; import com.ruoyi.bizsys.domain.AccountInfo; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml"); Object o = context.getBean(AccountInfo.class); System.out.println("4.使用"); ((ClassPathXmlApplicationContext) context).close(); } @Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml"); Object o = context.getBean(AccountInfo.class); System.out.println("6.使用"); ((ClassPathXmlApplicationContext) context).close(); } @Test public void test3(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml"); Object o = context.getBean(AccountInfo.class); System.out.println("8.使用"); ((ClassPathXmlApplicationContext) context).close(); } }
C. 2 输出结果
可以根据代码一步步进行累加,逐步理解10步骤的意思即可
转载:https://www.bilibili.com/video/BV1L14y1S7cf/?spm_id_from=333.337.search-card.all.click&vd_source=6451bb187cdcf8a07158b6acb30c5cac
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。