Spring容器Bean的实例化方式和声明周期

本文介绍Spring容器中Bean的三种实例化方式:直接构造、静态工厂方法及非静态工厂方法。此外,还详细解释了如何通过配置文件管理Bean的生命周期,包括初始化和销毁方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring容器中,Bean的实例化方式还是有三种的。第一种就是最普通的直接构造,第二种和第三种分别是使用工厂

生产模式来实例化Bean。下面来仔细说一下。

首先建立工厂类package com.bird.service.impl; public class PersonServerBeanFactory { public static PersonServerImpl creatPersonServerImpl(){ return new PersonServerImpl(); } public PersonServerImpl creatPersonServerImpl2(){ return new PersonServerImpl(); } } 分为两种,静态的和非静态的方法.然后构建Bean.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="com.bird.service.impl.PersonServerImpl" init-method="init" destroy-method="destory"></bean> <bean id="personService2" class="com.bird.service.impl.PersonServerBeanFactory" factory-method="creatPersonServerImpl"></bean> <bean id="personServiceFactory" class="com.bird.service.impl.PersonServerBeanFactory"></bean> <bean id="personService3" factory-bean="personServiceFactory" factory-method="creatPersonServerImpl2"></bean> </beans>
分别指定生产方式的生产函数就可以了,但是一般情况下还是是使用第一种方式最为常规.

下面说一下Spring构建的类的声明周期,一般来说,默认情况下是使用单例构造模式生成的,也就是说,两次请求

Spring容器同一个配置文件得到的类的实例是一个。但是如果想改变这种情况,也就是说每次获得的类的实例是不同

的实例,可以在bean.xml 文件中做如下配置.使用scope这个值.而且还有声明周期问题,如果希望Spring 容器在实例

化类的时候执行什么函数,然后在类将要被摧毁之前执行什么函数,也只需要在bean.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="com.bird.service.impl.PersonServerImpl" init-method="init" destroy-method="destory"></bean> </beans>测试函数如下

package junit.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bird.service.PersonServer; public class SpringTest { @Test public void test(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonServer s = (PersonServer)ctx.getBean("personService"); PersonServer s1 = (PersonServer)ctx.getBean("personService"); System.out.println(s==s1); } @Test public void test2(){ AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ctx.close(); } }
运行效果如下

2012-2-24 21:10:31 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]; startup date [Fri Feb 24 21:10:31 CST 2012]; root of context hierarchy 2012-2-24 21:10:31 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] 2012-2-24 21:10:32 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd20f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e9756 2012-2-24 21:10:32 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e9756: defining beans [personService]; root of factory hierarchy 初始化方法 true 我被销毁了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值