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
我被销毁了