Spring入门(五):Spring中bean的作用域
2019-08-30 09:15
1346 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zwwhnly/article/details/100150480
1. Spring中bean的多种作用域
在默认情况下,Spring应用上下文中所有的bean都是以单例(singleton)的形式创建的,即不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。
Spring定义了多种作用域,可以基于这些作用域创建bean:
- 单例(Singleton):在整个应用中,只创建bean的一个实例。
- 原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
- 会话(Session):在Web应用中,为每个会话创建一个bean实例。
- 请求(Request):在Web应用中,为每个请求创建一个bean实例。
单例是默认的作用域,如果要使用其它的作用域创建bean,需要使用
@Scope注解,该注解可以和
@Component,
@Service,
@Bean等注解一起使用。
2. 示例
为了更好的理解,我们通过具体的代码示例来理解下Singleton与ProtoType的区别。
2.1 新建Singleton类型的bean
package chapter03.scope; import org.springframework.stereotype.Service; @Service public class DemoSingletonService { }
因为Spring默认配置的Scope是Singleton,因此以上代码等价于以下代码:
package chapter03.scope; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public class DemoSingletonService { }
上面的代码也可以写成
@Scope("singleton")。
2.2 新建ProtoType类型的bean
如果是自动装配bean,语法为:
package chapter03.scope; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class DemoPrototypeService { }
上面的代码也可以写成
@Scope("prototype")。
如果是在Java配置类中声明bean,语法为:
@Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public DemoPrototypeService demoPrototypeService() { return new DemoPrototypeService(); }
如果是在xml中声明bean,语法为:
<bean id="demoPrototypeService" class="chapter03.scope.DemoPrototypeService" scope="prototype"/>
2.3 新建配置类
package chapter03.scope; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("chapter03.scope") public class ScopeConfig { }
2.4 测试
新建一个Main类,在main()方法中,分别从Spring容器中获取2次Bean,然后判断Bean的实例是否相等:
package chapter03.scope; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoSingletonService s1 = context.getBean(DemoSingletonService.class); DemoSingletonService s2 = context.getBean(DemoSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class); DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("s1 与 s2 是否相等:" + s1.equals(s2)); System.out.println("p1 与 p2 是否相等:" + p1.equals(p2)); context.close(); } }
运行结果如下:
从运行结果也可以看出,默认情况下即Singleton类型的Bean,不管调用多少次,只会创建一个实例。
3. 注意事项
Singleton类型的Bean,@Scope注解的值必须是:singleton。
ProtoType类型的Bean,@Scope注解的值必须是:prototype。
即使是大小写不一致也不行,如我们将DemoPrototypeService类的代码修改为:
package chapter03.scope; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service @Scope("PROTOTYPE") public class DemoPrototypeService { }
此时运行代码,就会报错:
4. 源码及参考
源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。
汪云飞《Java EE开发的颠覆者:Spring Boot实战》
5. 最后
打个小广告,欢迎扫码关注微信公众号:「申城异乡人」,定期分享Java技术干货,让我们一起进步。
相关文章推荐
- spring的入门(aop,ioc,bean作用域等)简介
- Spring学习一(入门和bean的作用域)
- Spring入门学习——设置Bean作用域
- spring中bean的作用域
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 【第三章】 DI 之 3.4 Bean的作用域 ——跟我学spring3
- Spring中bean的作用域
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- spring入门教程之bean的继承与自动装配详解
- Spring中Bean的作用域/状态/生命周期
- Spring入门之实例化Bean的三种方式
- Spring第二篇【Core模块之快速入门、bean创建细节、创建对象】
- Spring BeanBean的作用域及生命周期
- Spring入门之bean的属性
- Spring中Bean的作用域
- spring入门(4)---spring中Bean的生命周期总结
- Spring Bean的作用域之间有什么区别?
- SPRING中BEAN的作用域?
- Spring学习(二)IOC入门-Spring的bean管理(xml方式)
- Spring中Bean的作用域—Spring官方文档系列