Spring的Bean有多种scope作用域,Scope作用域是用来说明Spring容器是如何创建Bean实例的。
即scope="singleton",如果是用注解方式则可以不需要注明。
一 Spring的Scope的五种类型
① Singleton
表明容器中创建时只存在一个实例,所有引用此bean都是单一实例,也就是整个容器共享同一个实例。如果把容器当成一间屋子,那么该屋子里的电视只有一台,所有家庭成员共享这台电视。
使用XML格式来使用Singleton如下:
<bean id="role" class="spring.example.Singer" scope="singleton"/>
② Prototype
Scope取该值表明每次容器对请求方申请创建Bean的时候,都是创建新的对象,然后便释放对象的引用,容器每次返回请求方该对象的一个新的实例之后便把对象后继生命周期的管理工作,包括该对象的销毁交给请求方去处理。
Protorype使用方法:
<bean id="role" class="spring.example.Singer" scope="prototype"/>
③ Request
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效
Request的使用需要在初始化web的web.xml中做如下配置:
如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
如果是Servlet2.4以前的web容器,那么你要使用一个javax.servlet.Filter的实现:
<web-app>
..
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
当web.xml配置完成以后,就可以使用Request了:
<bean id="role" class="spring.example.Singer" scope="request"/>
④ Session
session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效
使用方法跟request类同
⑤ GlobalSession
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。
使用方法跟request类同
二 代码演示一下singleton和prototype
① 先编写一下Singleton的Bean,这里利用注解的方式,因为Singleton是默认的,不需要写出
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2017/7/8.
*/
@Service
public class DemoSingletonService {
}
编写Prototype的Bean,启动注解Scope,声明为Protoype
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2017/7/8.
*/
@Service
@Scope("prototype")
public class DemoPrototypeService {
}
② 编写配置类
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2017/7/8.
*/
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.scope")
public class ScopeConfig {
}
@ComponentScan注解会自动扫描包名下所有使用@Service @Component @Repository @Controller的类,并注册为Bean
import com.wisely.highlight_spring4.ch1.aop.DemoAnnotationService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Administrator on 2017/7/8.
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ScopeConfig.class);
DemoSingletonService demoSingletonService
= context.getBean(DemoSingletonService.class);
DemoSingletonService demoSingletonService2
= context.getBean(DemoSingletonService.class);
DemoPrototypeService demoPrototypeService
= context.getBean(DemoPrototypeService.class);
DemoPrototypeService demoPrototypeService2
= context.getBean(DemoPrototypeService.class);
System.out.println("测试两个Bean在single是否想等"+demoSingletonService.equals(demoSingletonService2));
System.out.println("测试两个Bean在prototype上是否相等"+
demoPrototypeService.equals(demoPrototypeService2));
context.close();
}
}
运行结果
测试两个Bean在Singleton是否相等true
测试两个Bean在prototype是否相等false