一、Bean的作用域
Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域。
Spring有6个作用域,最后四种是基于Spring WebMVC生效:
singleton
- 官方说明:(Default) Scopes a single bean definition to a single object instance for each SpringIoC container.
- 描述: 该作用域下的 Bean 在 IoC 容器中只存在一个实例:获取 Bean(即通过 applicationContext.getBean 等方法获取)及装配 Bean(即通过@Autowired注入)都是同一个对象。
- 场景: 通常无状态的 Bean 使用该作用域。无状态表示 Bean 对象的属性状态不需要更新。
- 备注: Spring 默认选择该作用域。
prototype
- 官方说明:Scopes a single bean definition to any number of object instances.
- 描述:每次对该作用域下的 Bean 的请求都会创建新的实例:获取Bean(即通过 applicationContext.getBean 等方法获取)及装配Bean(即通过
@Autowired
注入)都是新的对象实例。 - 场景:通常有状态的 Bean 使用该作用域。
使用 prototype 作用域需要慎重的思考,因为频繁创建和销毁 bean 会带来很大的性能开销。
request
- 官方说明:Scopes a single bean definition to the lifecycle of a single HTTP request. That is,each HTTP request has its own instance of a bean created off the back of a single beandefinition. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:每次 http 请求会创建新的 Bean 实例,类似于 prototype。
- 场景:一次 http 的请求和响应的共享 Bean。
- 备注:限定 SpringMVC 中使用。
session
- 官方说明:Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid inthe context of a web-aware Spring ApplicationContext.
- 描述:在一个 http session 中,定义一个 Bean 实例。
- 场景:用户回话的共享 Bean,比如:记录一个用户的登陆信息。
- 备注:限定 SpringMVC 中使用。
application
- 官方说明:Scopes a single bean definition to the lifecycle of a ServletContext. Only valid inthe context of a web-aware Spring ApplicationContext.
- 描述:在一个 http servlet Context 中,定义一个 Bean 实例。
- 场景:Web 应用的上下文信息,比如:记录一个应用的共享信息。
- 备注:限定 SpringMVC 中使用。
websocket
- 官方说明:Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in thecontext of a web-aware Spring ApplicationContext.
- 描述:在一个 HTTP WebSocket 的生命周期中,定义一个 Bean 实例。
- 场景:WebSocket 的每次会话中,保存了一个 Map 结构的头信息,将用来包裹客户端消息头。第一次初始化后,直到WebSocket 结束都是同一个 Bean。
- 备注:限定 Spring WebSocket 中使用。
二、prototype的使用示例
定义一个Bean,先通过类上的注解来定义为原型模式:
package org.example.dao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@Repository
@Scope("prototype")
public class PrototypeRepository {
}
点击——>什么是原型模式!!!
获取Bean实例:
package org.example;
import org.example.dao.PrototypeRepository;
import org.example.model.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//根据Spring配置文件路径创建容器:应用上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean每次获取都会创建一个新的Bean对象
PrototypeRepository prototypeRepository1 = (PrototypeRepository) context.getBean("prototypeRepository");
PrototypeRepository prototypeRepository2 = (PrototypeRepository) context.getBean("prototypeRepository");
System.out.printf("prototypeRepository: %s%n", prototypeRepository1 == prototypeRepository2);
User u7 = (User) context.getBean("u7");
System.out.println("七:"+u7);
//关闭容器
((ClassPathXmlApplicationContext) context).close();
}
}
在@Bean
注解的方法上也可以使用,以下为 定义的一个用户对象,可以查看注入的 prototype 对象是否为同一个:
@Bean
public User u7(PrototypeRepository p1, PrototypeRepository p2){
System.out.printf("user7: p1=%s, p2=%s, p1==p2: %s%n", p1, p2, p1==p2);
return new User();
}
执行结果