第一种:
1.使用类构造器实例化
<bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>
public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService = (PersonService) ctx.getBean("personService"); //调用 业务方法: personService.save(); } }
第二种:
2.使用静态工厂方法实例化
<bean id="personService2" class="cn.itm.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean"></bean>
-----------------------------------------------------------------------------------------------------------------------------
public staticPersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService = (PersonService) ctx.getBean("personService2"); //调用 业务方法: personService.save(); } }
第三种:使用实例工厂方法实例化:
<bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean"></bean>
相比第二种:把 static 关键字去掉了。
public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService = (PersonService) ctx.getBean("personService3"); //调用 业务方法: personService.save(); } }
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="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean> <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean" ></bean> </beans>public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。 PersonService personService1 = (PersonService) ctx.getBean("personService3"); PersonService personService2 = (PersonService) ctx.getBean("personService3"); System.out.println( personService1 == personService2); } }
打印结果:true。
<bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean" scope="prototype"></bean>
打印结果:false。
【1】singleton
在每个Spring IoC容器中一个bean定义只有一个对象实例。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
【2】prototype
每次从容器获取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进行实例化。。而加入 scope="prototype" 时,是在调用getBean方法的时候,实例化的 --> <bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean> </beans>import cn.itm.service.PersonService; public class PersonServiceBean implements PersonService{ public PersonServiceBean(){ System.out.println("我被实例化了"); } public void save(){ System.out.println("我是 save() 方法"); } }
测试类: public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } // 专门用来实例化 Spring 容器的。 @Test public void instanceSpring(){ // 在类路径下,寻找配置文件来实例化容器。 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // PersonService personService1 = (PersonService) ctx.getBean("personService3"); } }
打印结果:我被实例化了。
在每个Spring IoC容器中一个bean定义只有一个对象实例。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>

指定Bean的初始化方法和销毁方法
<bean id="xxx" class="cn.itcast.OrderServiceBean" init-method="init"
destroy-method="close"/>
<?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="cn.itm.service.impl.PersonServiceBean"
lazy-init="false" destroy-method="destory"></bean>
</beans>
package cn.itm.service.impl;
import cn.itm.service.PersonService;
public class PersonServiceBean implements PersonService{
public PersonServiceBean(){
System.out.println("我被实例化了");
}
public void save(){
System.out.println("我是 save() 方法");
}
public void destory(){
System.out.println("关闭");
}
}
AbstractApplicationContext
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
// 专门用来实例化 Spring 容器的。
@Test public void instanceSpring(){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ctx.close();
}
}