本节主要讲了三大块内容
1 bean的生命周期概念
2 bean的初始化和销毁的三种方式对比(代码演练)
3 总结
1 bean的生命周期概念
1.1 bean的定义:xml中关于bean的配置,bean的id和bean的class等。
1.2 bean的初始化:ioc容器启动的时候加载xml文件中的bean生成实例.
1.3 bean的使用:bean容器中取出bean的实例并使用
1.4 bean销毁:指的是bean销毁时回收由这个bean创建的所有bean实例。
2 bean的初始化和销毁的三种方式对比(代码演练)
2.1 xml配置init-method方法
init-method方法实现类:
package com.imooc.lifecycle; public class BeanLifeCycle { //单独bean方法初始化 public void start(){ System.out.println("单独bean,init方法执行"); } public void stop(){ System.out.println("单独bean,destroy方法执行"); } }
init-method方法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" default-init-method="init" default-destroy-method="destroy"> <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop"></bean> </beans>
测试类:
package com.imooc.lifecycle; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.cglib.core.Block; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestLifeCycle extends UnitTestBase{ public TestLifeCycle() { super("classpath*:spring-beanLifeCycle.xml"); // TODO Auto-generated constructor stub } @Test public void testLifeCycle(){ super.getbean("beanLifeCycle"); } }
2.2 实现接口 bean的初始化和销毁 方法
实现类:
package com.imooc.lifecycle; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BeanLifeCycle implements InitializingBean,DisposableBean{ /** * 实现接口,覆盖bean 的 初始化和销毁方法 */ @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的初始化方法"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的销毁方法"); } /*//单独bean方法初始化 public void start(){ System.out.println("单独bean,init方法执行"); } public void stop(){ System.out.println("单独bean,destroy方法执行"); }*/ }
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" default-init-method="init" default-destroy-method="destroy"> <!-- init-method="start" destroy-method="stop" --> <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"></bean> </beans>
测试类:
package com.imooc.lifecycle; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.cglib.core.Block; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestLifeCycle extends UnitTestBase{ public TestLifeCycle() { super("classpath*:spring-beanLifeCycle.xml"); // TODO Auto-generated constructor stub } @Test public void testLifeCycle(){ super.getbean("beanLifeCycle"); } }
2.3 全局初始化销毁方法
实现类:
package com.imooc.lifecycle; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BeanLifeCycle { /** * 全局初始化销毁方法 */ //init方法名和xml中的配置相关 private void init() { // TODO Auto-generated method stub System.out.println("全局初始化方法!"); } //destroy方法名和xml中的配置相关 private void destroy() { // TODO Auto-generated method stub System.out.println("全局销毁方法!"); } /** * 实现接口,覆盖bean 的 初始化和销毁方法 */ /*@Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的初始化方法"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的销毁方法"); }*/ /*//单独bean方法初始化 public void start(){ System.out.println("单独bean,init方法执行"); } public void stop(){ System.out.println("单独bean,destroy方法执行"); }*/ }
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" default-init-method="init" default-destroy-method="destroy"> <!-- init-method="start" destroy-method="stop" --> <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"></bean> </beans>
测试类:
package com.imooc.lifecycle; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.cglib.core.Block; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestLifeCycle extends UnitTestBase{ public TestLifeCycle() { super("classpath*:spring-beanLifeCycle.xml"); // TODO Auto-generated constructor stub } @Test public void testLifeCycle(){ super.getbean("beanLifeCycle"); } }
2.4 三种初始化和销毁方法同时执行(注意destroy方法):猜猜会输出什么?
实现类:
package com.imooc.lifecycle; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BeanLifeCycle implements InitializingBean,DisposableBean{ /** * 全局初始化销毁方法 */ //init方法名和xml中的配置相关 private void init() { // TODO Auto-generated method stub System.out.println("全局初始化方法!"); } //destroy方法名和xml中的配置相关 private void destroy2() { // TODO Auto-generated method stub System.out.println("全局销毁方法!"); } /** * 实现接口,覆盖bean 的 初始化和销毁方法 */ @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的初始化方法"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("实现接口,这里完成bean的销毁方法"); } //单独bean方法初始化 public void start(){ System.out.println("单独bean,init方法执行"); } public void stop(){ System.out.println("单独bean,destroy方法执行"); } }
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" default-init-method="init" default-destroy-method="destroy2"> <!-- init-method="start" destroy-method="stop" --> <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop"></bean> </beans>
测试类:
package com.imooc.lifecycle; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.cglib.core.Block; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestLifeCycle extends UnitTestBase{ public TestLifeCycle() { super("classpath*:spring-beanLifeCycle.xml"); // TODO Auto-generated constructor stub } @Test public void testLifeCycle(){ super.getbean("beanLifeCycle"); } }
测试结果:
二月 24, 2019 8:42:21 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Sun Feb 24 08:42:21 CST 2019]; root of context hierarchy 二月 24, 2019 8:42:21 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanLifeCycle.xml] 实现接口,这里完成bean的初始化方法 单独bean,init方法执行 二月 24, 2019 8:42:22 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Sun Feb 24 08:42:21 CST 2019]; root of context hierarchy 实现接口,这里完成bean的销毁方法 单独bean,destroy方法执行
3 总结
bean有默认的初始化销毁方法a,实现接口初始化销毁方法b,配置bean的初始化销毁方法c。
3.1 如果b和c没有声明实现,那么会默认执行a
3.2 如果有bc任何一种方法,不管是否有默认方法a,都不会执行方法a
3.3 如果至少有bc两种方法,那么会优先执行方法b。
原文地址:https://www.cnblogs.com/1446358788-qq/p/10425291.html
时间: 2024-11-07 11:03:49