您的位置:首页 > 编程语言 > Java开发

Spring bean的作用域之间有什么区别

2018-09-25 19:22 78 查看

Spring的 bean有5种作用域分别是:singleton、prototype、request、session和GlobalSession

简单介绍:

spring 起初的版本只有singleton,也就是是否是单例模式。

作用域包括:

1 、singleton全局只有一个实例
2、 prototype每次调用产生一个新的实例在web使用的时候还有三个作用域,但是必须在web.xml中注册一个RequestContextListener , 目的是为了设置每次请求开始和结束都可以使spring得到相应的事件。
3、 request每次请求产生一个beansession每个用户session可以产生一个新的bean,不同用户之间的bean互相不影响globalSession作用和session类似,只是使用portlet的时候使用。
》》》 测试用例因为平时使用SPRING MVC开发的时候比较多,有必要了解清楚怎么去调用这几种作用域。

1,定义不同作用域的java类

Java代码
@Component
@Scope( "session")
public class SessionObj {
}
@Component
@Scope( "request")
public class RequestObj {
}
@Component
@Scope( "prototype")
public class PrototypeObj {
}
@Component
@Scope( "singleton")
public class SingletonObj {
}

2,注入到controller,由于controller是单例的,因此必须通过实现ApplicationContextAware接口,直接从容器中取出对象。

因此测试的controller是:

@Controller
public class IndexController implements ApplicationContextAware {
private RequestObj Request Obj;
private SessionObj SessionObj;
private PrototypeObj PrototypeObj;
private SingletonObj SingletonObj;
private ApplicationContext applicationContext;
@RequestMapping("/")
@ResponseBody
public String index() {
print();
return "Welcome";
}
public void print() {
System.out.println("first  time singleton is :" + getSingletonObj());
System.out.println("second time singleton is :" + getSingletonObj());
System.out.println("first  time prototype is :" + getPrototypeObj());

System.out.println("second time prototype is :" + getPrototypeObj());
System.out.println("first  time request is :" + getRequestObj());
System.out.println("second time request is :" + getRequestObj());
System.out.println("first  time session is :" + getSessionObj());
System.out.println("second time session is :" + getSessionObj());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public RequestObj getRequestObj() {
return applicationContext.getBean(RequestObj.class);
}
public void setRequestObj(RequestObj requestObj) {
RequestObj = requestObj;
}
public SessionObj getSessionObj() {
return applicationContext.getBean(SessionObj.class);
}
public void setSessionObj(SessionObj sessionObj) {
SessionObj = sessionObj;
}
public PrototypeObj getPrototypeObj() {
return applicationContext.getBean(PrototypeObj.class);
}
public void setPrototypeObj(PrototypeObj prototypeObj) {
PrototypeObj = prototypeObj;
}
public SingletonObj getSingletonObj() {
return applicationContext.getBean(SingletonObj.class);
}
public void setSingletonObj(SingletonObj singletonObj) {
SingletonObj = singletonObj;
}
}

3,运行结果: Java代码 //使用chrome第一次打印数据:

first  time singleton is :com.fb.po.SingletonObj@1e3223e
second time singleton is :com.fb.po.SingletonObj@1e3223e
first  time prototype is :com.fb.po.PrototypeObj@3e683f
second time prototype is :com.fb.po.PrototypeObj@12e18d7
first  time request is :com.fb.po.RequestObj@1d45706
second time request is :com.fb.po.RequestObj@1d45706
first  time session is :com.fb.po.SessionObj@9a6b2e
second time session is :com.fb.po.SessionObj@9a6b2e
//使用chrome打印第二次数据
first  time singleton is
:com.fb.po.SingletonObj@1e3223e
second time singleton is :com.fb.po.SingletonObj@1e3223e
first  time prototype is :com.fb.po.PrototypeObj@122e5be
second time prototype is :com.fb.po.PrototypeObj@192add
first  time request is :com.fb.po.RequestObj@4d1b6c
second time request is :com.fb.po.RequestObj@4d1b6c
first  time session is :com.fb.po.SessionObj@9a6b2e
second time session is :com.fb.po.SessionObj@9a6b2e
//使用IE打印第三次数据
first  time singleton is
:com.fb.po.SingletonObj@1e3223e
second time singleton is :com.fb.po.SingletonObj@1e3223e
first  time prototype is :com.fb.po.PrototypeObj@10f1ecb
second time prototype is :com.fb.po.PrototypeObj@1aeb990
first  time request is :com.fb.po.RequestObj@18a1e7
second time request is :com.fb.po.RequestObj@18a1e7
first  time session is :com.fb.po.SessionObj@12d5c55
second time session is :com.fb.po.SessionObj@12d5c55

4,结果分析:从结果来看,单例的bean的三次的数据都是打印一样的(默认的bean的级别就是单例);prototype的bean每次的数据都是不一样的,每次请求的时候调用两次结果都不一样。request的bean在每次request的时候都不一致,但是同一次request返回的数据是一致的。session的bean在前两次结果一致,最后一次数据不一致,和session的节奏是一致的。

5,欠缺

结果分析:

singleton , request 和session结果都是上面一致,但是prototype结果是和request效果一致的。所以如果要拿到prototype类型的数据的话,应该每次需要都去容器中去拿,而不是注入。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐