在<一>的基础上,修改
<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl"></bean>
为
1、 <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" ></bean>
默认情况下是单例的,即通过getBean方法得到的对象是同一个对象。这时候会在容器实例化时候就实例化类
2、 <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" scope="prototype" ></bean>
这种情况下,每调用一次getBean方法就会得到一个新的对象。会有延迟加载功能,这时候是在调用getBean时候才会实例化类
3、 <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" lazy-init="true" ></bean>
默认情况下是单例的,即通过getBean方法得到的对象是同一个对象。这时候会在调用getBean时候才会实例化类,但是多次调用只会进行一次实例化
4、<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" lazy-init="false"
init-method="init"></bean>
默认情况下是单例的,无延迟加载,会在容器实例化时候实例化类,然后再执行init方法(容器反射机制)
1、2和3的测试,在SpringTest类中,修改添加如下代码即可:
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的
personService.save();
PersonService personService0 = (PersonService)ctx.getBean("personService");
PersonService personService1 = (PersonService)ctx.getBean("personService");
System.out.println(personService0==personService1);
ctx.close();
4的测试:在PersonServiceImpl实现类中,添加该方法即可:
public void init(){
System.out.println("我是初始化方法");
}
完成容器实例化时候类什么时候实例化的测试。
-------------------------------------------------------------------------------------------------------------------------------------
接下来,在讲下如何通过静态工厂方法和实例工厂方法来进行实例化
1、新建PersonServiceImplFactory类:
package cn.chen.service;
import cn.chen.service.impl.PersonServiceImpl;
public class PersonServiceImplFactory {
//静态工厂方法实例化
public static PersonServiceImpl createPersonServiceImpl(){
return new PersonServiceImpl();
}
//实例工厂方法实例化
public PersonServiceImpl createPersonServiceImpl2(){
return new PersonServiceImpl();
}
}
2、配置beans.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="cn.chen.service.impl.PersonServiceImpl" init-method="init"></bean>
<!--class表示类的路径,id表示由spring容器来创建该类的一个对象-->
<!-- 静态工厂方法实例化,该方法返回PersonServiceBean -->
<bean id="personService2" class="cn.chen.service.PersonServiceImplFactory"
factory-method="createPersonServiceImpl"/>
<!-- 实例工厂方法实例化,factory-method="createPersonServiceBean2"返回PersonServiceBean
多了一步factory-bean-->
<bean id="personServiceFactory" class="cn.chen.service.PersonServiceImplFactory"/>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceImpl2"/>
</beans>
3、测试,SpringTest类
package cn.chen.test;
import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.chen.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception{
}
@Test public void instanceSpring(){
//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//读取spring配置文件
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的
personService.save();
PersonService personService0 = (PersonService)ctx.getBean("personService");
PersonService personService1 = (PersonService)ctx.getBean("personService");
System.out.println(personService0==personService1);
PersonService personService2 = (PersonService)ctx.getBean("personService2");
personService2.save();
PersonService personService3 = (PersonService)ctx.getBean("personService3");
personService3.save();
ctx.close();
}
}
控制台显示:
我是初始化方法
我是save()方法
true
我是save()方法
我是save()方法
源码下载地址: