scope的属性值
属性值 | 作用 |
---|---|
singleton | 默认单例 |
prototype | 原型每调用一次getBean()方法则获取一个新的Bean对象 , 每次注入的时候都是新对象 |
request | 一个请求对应一个Bean仅限于在WEB应用中使用 , 需要引入web的框架如SpringMvc |
(global) session | 一个会话对应一个Bean仅限于在WEB应用中使用 , 需要引入web的框架如SpringMvc |
global session | portlet应用中专用的, portlet和servlet都是规范, servlet运行在servlet容器中如Tomcat, portlet运行在portlet容器中 |
application | 一个应用对应一个Bean仅限于在WEB应用中使用 |
websocket | 一个websocket生命周期对应一个Bean仅限于在WEB应用中使用 |
自定义scope | 很少使用 |
scope属性值singleton(单例)
Spring的IoC容器中默认情况下Bean对象是单例的
public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的无参数构造方法执行");
}
}
<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="sb" class="com.powernode.spring6.beans.SpringBean" />
beans>
@Test
public void testScope(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
//com.powernode.spring6.beans.SpringBean@5b239d7d
System.out.println(sb1);
//com.powernode.spring6.beans.SpringBean@5b239d7d
SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb2);
}
scope属性值prototype(多例)
如果想让Spring的Bean对象以多例的形式存在,可以在bean标签中指定scope属性的值为prototype
<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="sb" class="com.powernode.spring6.beans.SpringBean" scope="prototype" />
beans>
@Test
public void testScope(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
//com.powernode.spring6.beans.SpringBean@29445a7
SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb1);
//com.powernode.spring6.beans.SpringBean@67d48005
SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb2);
}
自定义线程级别Scope(了解)
第一步: 自定义Scope实现Scope接口,实现在同一个线程中获取的Bean都是同一个, 跨线程获取的是不同的Bean对象
org.springframework.context.support.SimpleThreadScope
第二步: 使用CustomScopeConfigurer
内置类将自定义的Scope注册到Spring容器中
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="myThread">
<bean class="org.springframework.context.support.SimpleThreadScope"/>
entry>
map>
property>
bean>
第三步: 使用自定义线程的Scope
<bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="myThread" />
@Test
public void testCustomScope(){
//主线程
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
//com.powernode.spring6.beans.SpringBean@5b236d7d
System.out.println(sb1);
//com.powernode.spring6.beans.SpringBean@5b236d7d
System.out.println(sb2);
// 启动新线程
new Thread(new Runnable() {
@Override
public void run() {
SpringBean a = applicationContext.getBean("sb", SpringBean.class);
SpringBean b = applicationContext.getBean("sb", SpringBean.class);
//com.powernode.spring6.beans.SpringBean@29445a6
System.out.println(a);
//com.powernode.spring6.beans.SpringBean@29445a6
System.out.println(b);
}
}).start();
}