1. Bean的Scope

scope描述Spring容器如何新建Bean的实例。通过注解@Scope实现,取值有:

a. Singleton:一个Spring容器中只有一个Bean的实例。此为Spring的默认配置,全容器共享一个实例。

b. Prototype:每次调用新建一个Bean的实例

c. Request:Web项目中,给每一个Http Request新建一个Bean实例

d. Session:Web项目中,给每一个Http Session新建一个Bean实例

e. GlobalSession:这个只在portal应用中有用,给每一个global http session新建一个Bean实例。

示例:演示Singleton和Prototype分别从Spring容器中获取2次Bean,判断Bean的实例是否相等

1) 编写Singleton的Bean

  1 package com.ws.study.scope;
  2 
  3 import org.springframework.stereotype.Service;
  4 
  5 // 默认为Singleton,等同于@Scope("singleton")
  6 @Service
  7 public class DemoSingletonService {
  8 }
  9 
View Code

相关文章:

  • 2021-03-31
  • 2021-10-06
  • 2022-12-23
  • 2021-10-28
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2020-07-10
  • 2022-12-23
  • 2021-05-12
  • 2021-06-04
  • 2021-06-07
  • 2021-06-22
相关资源
相似解决方案