Spring管理的Bean的生命周期

我们在实例化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方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值