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