一、第一种实例化的方法:使用类构造器实例化
1、这中实例化方法在前面以及用到了,这里不再细讲。
二、第二种实例化方法:使用静态工厂方法实例化
1、创建一个类 PresonServiceFactoryImpl,代码如下:
![]()
package com.learn.service.impl;
public class PresonServiceFactoryImpl {
public static PresonServiceImpl createPresonServiceImpl(){
return new PresonServiceImpl();
}
}
其中的 PresonServiceImpl 是我们前面讲到的一个类。这里不再赘余。
2、既然实例化的方式做了改变,那么xml文件的代码也会与之前的有所区别,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.xsd">
<!-- factory-method 属性的值是工厂方法中创建实例的方法名 -->
<!-- 使用静态工厂方法实例化 -->
<bean id="personService" class="com.learn.service.impl.PresonServiceFactoryImpl" factory-method="createPresonServiceImpl"></bean>
</beans>
3、实例化,代码如下:
![]()
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.service.PresonService;
public class TestSpring {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void initContainerSpring() {
//实例化spring容器 (使用类构造器实例化)
ApplicationContext ctx = new ClassPathXmlApplicationContext("com//learn//spring//learn1.xml");
//从spring容器中获取bean getBean("personService")字符串要与spring配置文件learn1.xml属性id的值一致
@SuppressWarnings("unused")
//使用静态工厂方法实例化
PresonService personService = (PresonService)ctx.getBean("personService");
//调用save()
personService.save();
}
}
4、运行测试。
三、使用实例工厂方法实例化
1、在 类 PresonServiceFactoryImpl 里添加方法:
![]()
public PresonServiceImpl createPresonServiceImpl2(){
return new PresonServiceImpl();
}
与之前的方法相比就是缺少了一个static 关键字。
2、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.xsd">
<!-- 使用实例工厂方法实例化 -->
<bean id="presonServiceFactory" class="com.learn.service.impl.PresonServiceFactoryImpl"></bean>
<!-- factory-bean属性值 "presonServiceFactory" 与 id属性值 "presonServiceFactory" 一致 -->
<bean id="personService" factory-bean="presonServiceFactory" factory-method="createPresonServiceImpl2"></bean>
</beans>
3、实例化,代码如下:
![]()
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.service.PresonService;
public class TestSpring {
@Test
public void initContainerSpring() {
//实例化spring容器 (使用类构造器实例化)
ApplicationContext ctx = new ClassPathXmlApplicationContext("com//learn//spring//learn1.xml");
//从spring容器中获取bean getBean("personService")字符串要与spring配置文件learn1.xml属性id的值一致
//使用实例工厂方法实例化
PresonService personService = (PresonService)ctx.getBean("personService");
//调用save()
personService.save();
}
}
4、运行测试。
四、bean 的作用域
默认情况下 bean 的作用域是单实例,如下
![]()
<bean id="personService" name="" class="com.learn.service.impl.PresonServiceImpl"></bean>
相当于:
![]()
<bean id="personService" name="" class="com.learn.service.impl.PresonServiceImpl" scope="singleton"></bean>
这种作用域情况下的实例时同一个,一下的代码会更好的理解:
![]()
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.service.PresonService;
public class TestSpring {
@Test
public void initContainerSpring() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("com//learn//spring//learn1.xml");
PresonService personService = (PresonService)ctx.getBean("personService");
PresonService personService2 = (PresonService)ctx.getBean("personService");
System.out.println(personService == personService2);
}
}
控制台打印出来的是 true 。
其余的大家可以查看:
http://zyc1006.iteye.com/blog/1127614
这里面有详细的讲解。