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