Spring 该目的是通过默认单身创建的对象
设定Bean范围。由Bean美元Scope财产
Scope取值范围:
Singleton:单例
proptotype:非单例
Request:创建该Bean,并调用request.setAttribute(“beanId”,beanObj);
Session:创建该Bean,并调用request.getSession().setAttribute(“beanId”,beanObj);
globalSession:全局Session分布式server
測试代码
public class InitBeanApp {
@Test
public void show() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicatioContext.xml");
Bean1 bean1 = (Bean1) ac.getBean("bean2");
Bean1 bean2 = (Bean1) ac.getBean("bean2");
Bean1 bean3 = (Bean1) ac.getBean("bean3");
Bean1 bean4 = (Bean1) ac.getBean("bean3");
System.out.println(bean1);
System.out.println(bean2);
System.out.println(bean3);
System.out.println(bean4);
}
}
配置代码
<!-- bean的作用域 -->
<!-- scope:用于设置bean的作用域 默认值:singleton 单例-->
<bean id="bean2" class="com.hao947.bean.Bean1" scope="singleton">
</bean>
<!-- bean的作用域 -->
<!-- scope:对于设置bean范围 默认:prototype -->
<bean id="bean3" class="com.hao947.bean.Bean1" scope="prototype">
</bean>