springBoot设置Bean的作用域


作用域:

singleton:单列模式

prototype:原型模式

 

注:spring默认为单列模式

 

例子1:@Score("singleton")

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/*
设置Bean在spring容器中以单列模式存在
 */
@Scope("singleton")
@Component
public class SingleScopeTest {
}

 

例子2:@Scope("prototype")

1 import org.springframework.context.annotation.Scope;
2 import org.springframework.stereotype.Component;
3 /*
4 设置Bean在spring容器中以多例形式存在
5  */
6 @Scope("prototype")
7 @Component
8 public class PrototypeScoreTest {
9 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM