我们在实例化bean的时候会遇到一个疑问:在什么时候得到bean
,是在得到spring容器的时候同时实例化bean还是使用getBean方法之后得到bean对象?下面,我们就来讨论一下spring管理的bean的生命周期。
我们讨论spring管理的bean的生命周期主要指的是两个方法,init(),destroy()
实例化情况
配置文件是单例模式的时候(bean对象是同一个对象),bean对象是被在spring容器实例化的时候得到
代码演示(这里指演示单例模式配置文件的一种形式)
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-2.5.xsd">
<bean id="personService" class="yanxi.service.implement.PersonServiceBean"
</bean>
</beans>
springTest.java
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yanxi.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
}
PersonService.java
package yanxi.service.implements;
import yanxi.service.PersonService;
public class PersonServiceBean implements PersonService {
public void init(){
System.out.println("初始化");
}
public PersonServiceBean(){
System.out.println("我被实例化了");
}
public void save(){
System.out.println("我是save()方法");
}
}
输出结果是:我被实例化了
在检测spring容器和bean得到的先后顺序的时候,暂时不要getBean()
根据结果显示,在默认情况(单例模式)下,得到spring容器的时候就实例化了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-2.5.xsd">
<bean id="personService" class="yanxi.service.implement.PersonServiceBean" scope="prototype"
</bean>
</beans>
springTest.java
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yanxi.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
}
PersonService.java
package yanxi.service.implements;
import yanxi.service.PersonService;
public class PersonServiceBean implements PersonService {
public void init(){
System.out.println("初始化");
}
public PersonServiceBean(){
System.out.println("我被实例化了");
}
public void save(){
System.out.println("我是save()方法");
}
}
输出结果:
根据输出结果显示,spring容器在费单例模式情况下,是在spring容器实例化之后得到的bean对象,可以将springTest.java做以下更改就可以看出
springTest.java
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yanxi.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService per = (PersonService)ctx.getBean("personService");
per.save();
}
}
输出结果为:
我被实例化了
我是save()方法
bean对象还有一个属性能够延迟初始化lazy-init=”false/true”
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-2.5.xsd">
<bean id="personService" class="yanxi.service.implement.PersonServiceBean" lazy-init="false"
</bean>
</beans>
效果和单例模式情况下是一样的
lazy-init="true
和非单例模式情况下是一样的
如果我们想要将所有的bean对象都是在spring实例化之后得到,那么可以将beans的lazy-init属性设置为true,不过一般不建议这么做
初始化方法init和destroy
<?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-2.5.xsd">
<bean id="personService" class="yanxi.service.impl.PersonServiceBean" lazy-init="false"
init-method="init" destroy-method="destory">
</bean>
</beans>
init-method=”init” destroy-method=”destory”
当容器被初始化之后执行这个init方法,当spring容器被销毁之后,执行destory方法