关于Spring 的Bean的作用域问题,这里简单解释
spring 4.x 中,scorp 显示有4个属性 分别是singleton(默认),prototype,session,request
singleton,单例的意思,IOC容器在初始化时会返回Bean的一个共享实例。只要bean的请求ID(在内存栈中的地址)与该bean相匹配,都会返回同一实例。
prototype,与单例相反,它每一次都会创建一个新的Bean
session,就是每一次HTTP请求都会在当前的session中创建一个Bean,并且该bean只在当前的session请求中有效
request,每一次HTTP请求都会创建一个Bean,并且该bean只在当前的request请求中有效
下面通过代码详细一点解释下singleton,和prototype,其实理解一下文字也够
了
新建一个Person类:
package jaky.bean;
import java.util.List;
import java.util.List;
public class Person{private String name;private List<House> houses;public Person(){}public Person(String name, List<House> houses){this.name = name;this.houses = houses;}public String getName(){return name;}public void setName(String name){this.name = name;}public List<House> getHouses(){return houses;}public void setHouses(List<House> houses){this.houses = houses;}@Overridepublic String toString(){return "Person [name=" + name + ", houses=" + houses + "]";}}
新建一个House类:(每个人都有家)
package jaky.bean;
public class House{private String address;public House(){super();}public String getAddress(){return address;}public void setAddress(String address){this.address = address;}@Overridepublic String toString(){return "House [address=" + address + "]";}}
配置文件:
<?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="house1" class="jaky.bean.House">
<property name="address" value="guangzhou"></property>
</bean>
<bean id="house2" class="jaky.bean.House">
<property name="address" value="shenzhen"></property>
</bean>
<bean id="person" class="jaky.bean.Person" scope="prototype">
<property name="name" value="Jaky"></property>
<property name="houses">
<list>
<ref bean="house1"/>
<ref bean="house2"/>
</list>
</property>
</bean>
</beans>
<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="house1" class="jaky.bean.House">
<property name="address" value="guangzhou"></property>
</bean>
<bean id="house2" class="jaky.bean.House">
<property name="address" value="shenzhen"></property>
</bean>
<bean id="person" class="jaky.bean.Person" scope="prototype">
<property name="name" value="Jaky"></property>
<property name="houses">
<list>
<ref bean="house1"/>
<ref bean="house2"/>
</list>
</property>
</bean>
</beans>
Main 方法:
package jaky.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person1 = (Person)ctx.getBean("person");
System.out.println(person1);
Person person2= (Person)ctx.getBean("person");
System.out.println(person2);
System.out.println(person1==person2);
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person1 = (Person)ctx.getBean("person");
System.out.println(person1);
Person person2= (Person)ctx.getBean("person");
System.out.println(person2);
System.out.println(person1==person2);
}
}
当把配置文件中的scope值改为 singleton时,(或者不填)
运行Main.java 结果
输出true,说明两个person完全相同。
另外在Person类中的构造其中加入一句System.out.println("person初始化");
运行你会发现:
构造器只被调用了一次,所以大家应该有点理解singleton了吧。
当把scope 属性改为prototype时,相信运行的结果你们能理解的
初始化了两次。
session和request 就不做详细解释了,大家可以改一下,然后部署到tomcat测试一下