Bean的作用域

代码案例

代码:

//Component类注入了一个Bean,返回的值是 1号小明
@Component
public class UserComponent {
    @Bean
    public User getUser1() {
        User user = new User(1, "小明");
        System.out.println("数据信息为:" + user);
        return user;
    }
}
//然后再Controller层注入getUser这个方法,并且修改name为 花花
@Controller
public class UserController {
    @Autowired
    User getUser1;

    public void getGetUser1() {
        System.out.println("Controller层进行修改");
        getUser1.setName("花花");
        System.out.println(getUser1);
    }
}
//在Service层再进行注入这个User那么这个User的name属性是小明呢还是花花呢?
public class UserService {
    @Autowired
    User getUser1;

    public void getGetUser1() {
        System.out.println("UserService层注入的User1的信息为:");
        System.out.println(getUser1);
    }
}
//下面在main方法中执行这两个方法,看看下面打印结果!
public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserController userController = context.getBean("userController", UserController.class);
        userController.getGetUser1();
        UserService userService = context.getBean("userService",UserService.class);
        userService.getGetUser1();
    }
}
//打印结果:我们可以看到 Service层注入的user也是Controller层修改后的结果
        数据信息为:User{id=1, name='小明'}
        Controller层进行修改
        User{id=1, name='花花'}
        UserService层注入的User1的信息为:
        User{id=1, name='花花'}    

原因分析

操作以上问题的原因是因为 Bean 默认情况下是单例状态(singleton),也就是所有人的使用的都是同一个对象,之前我们学单例模式的时候都知道,使用单例可以很大程度上提高性能,所以在 Spring 中Bean 的作用域默认也是 singleton 单例模式。

Bean的作用域

什么是Bean的作用域呢?

可以理解为“行为模式”,而Bean是有六中行为模式的,也就是六种作用域

Bean 的作用域是指 Bean 在 Spring 整个框架中的某种行为模式比如 singleton 单例作用域,就表示 Bean 在整个 Spring 中只有一份,它是全局共享的,那么当其他人修改了这个值之后,那么另一个人读取到的就是被修改的值。

Bean的六种作用域

Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域。Spring有 6 种作用域,最后四种是基于 Spring MVC 生效的,注意后 4 种状态是 Spring MVC 中的值,在普通的 Spring 项目中只有前两种。
  • singleton:单例作用域(默认)

  • prototype:原型作用域(多例作用域)

  • request:请求作用域

  • session:回话作用域

  • application:全局作用域

  • websocket:HTTP WebSocket 作用域

singleton

  • 官方说明 :(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

  • 描述 :该作用域下的Bean在IoC容器中只存在一个实例:获取Bean及装配Bean都是同一个对象。

  • 场景 :通常无状态的Bean使用该作用域。无状态表示Bean对象的属性状态不需要更新

  • 备注 :Spring默认选择该作用域

prototype

  • 官方说明 :Scopes a single bean definition to any number of object instances.

  • 描述 :每次对该作用域下的Bean的请求都会创建新的实例:获取Bean及装配Bean都是新的对象实例。

  • 场景 :通常有状态的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 in the 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 in the 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 the context of a web-aware Spring ApplicationContext.

  • 描述:在一个HTTP WebSocket的生命周期中,定义一个Bean实例

  • 场景:WebSocket的每次会话中,保存了一个Map结构的头信息,将用来包裹客户端消息头。第一次初始化后,直到WebSocket结束都是同一个Bean。

  • 备注限定Spring WebSocket中使

singleton VS application

  • singleton 是 Spring Core 的作用域;application 是 Spring Web 中的作用域;

  • singleton 作用于 IoC 的容器, application 作用于 Servlet 容器。

@Scope 有两种设置方式

@Scope注解是用来自定义作用域的,默认是单例模式

① 直接设置值:@Scope("prototype")

② 使用枚举设置:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shn!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值