博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中bean的范围
阅读量:7246 次
发布时间:2019-06-29

本文共 2649 字,大约阅读时间需要 8 分钟。

Spring中有5种bean的范围:

5 types of bean scopes supported :

  1. singleton – Return a single bean instance per Spring IoC container 这个范围也是默认的
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *

P.S * means only valid in the context of a web-aware Spring ApplicationContext

我们看个关于singleton and prototype.的例子:

1
2
3
4
5
6
7
8
9
10
11
12
public 
class 
CustomerService
{
    
String message;
  
    
public 
String getMessage() {
        
return 
message;
    
}
  
    
public 
void 
setMessage(String message) {
        
this
.message = message;
    
}
}

1. Singleton example

1
2
3
4
5
6
7
8
9
<beans xmlns=
""
    
xmlns:xsi=
""
    
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
    
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
       
<bean id=
"customerService"
            
class
=
"com.mkyong.customer.services.CustomerService" 
/>
  
</beans>

  运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public 
class 
App
{
    
public 
static 
void 
main( String[] args )
    
{
        
ApplicationContext context =
         
new 
ClassPathXmlApplicationContext(
new 
String[] {
"Spring-Customer.xml"
});
  
        
CustomerService custA = (CustomerService)context.getBean(
"customerService"
);
        
custA.setMessage(
"Message by custA"
);
        
System.out.println(
"Message : " 
+ custA.getMessage());
  
        
//retrieve it again
        
CustomerService custB = (CustomerService)context.getBean(
"customerService"
);
        
System.out.println(
"Message : " 
+ custB.getMessage());
    
}
}

  输出为:

Message : Message by custAMessage : Message by custA

2. Prototype example

1
2
3
4
5
6
7
8
9
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
   
<
bean 
id="customerService" class="com.mkyong.customer.services.CustomerService"
         
scope="prototype"/>
  
</
beans
>

  运行输出为:

Message : Message by custAMessage : null 也可以使用注解来做这些事情:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package 
com.mkyong.customer.services;
  
import 
org.springframework.context.annotation.Scope;
import 
org.springframework.stereotype.Service;
  
@Service
@Scope
(
"prototype"
)
public 
class 
CustomerService
{
    
String message;
  
    
public 
String getMessage() {
        
return 
message;
    
}
  
    
public 
void 
setMessage(String message) {
        
this
.message = message;
    
}
}

  

1
2
3
4
5
6
7
8
9
10
11
<beans xmlns=
""
    
xmlns:xsi=
""
    
xmlns:context=
""
    
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
    
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
    
http:
//www.springframework.org/schema/context
    
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd">
  
       
<context:component-scan base-
package
=
"com.mkyong.customer" 
/>
  
</beans>

转载地址:http://vbbbm.baihongyu.com/

你可能感兴趣的文章
素数之魂——黎曼和他的伟大猜想
查看>>
【Java深入学习系列】之CPU的分支预测(Branch Prediction)模型
查看>>
《Axure RP7网站和APP原型制作从入门到精通》一1.4 交互基础
查看>>
腾讯Android自动化测试实战
查看>>
利用Java动态生成 PDF 文档
查看>>
《Unreal Engine 4蓝图可视化编程》一2.4 小结
查看>>
在 Linux 下使用 TCP 封装器来加强网络服务安全
查看>>
《操作系统真象还原》——0.28 MBR、EBR、DBR和OBR各是什么
查看>>
8 个构建容器应用的最佳实践
查看>>
《JavaScript面向对象编程指南》——2.6 条件与循环
查看>>
awk 系列:如何使用 awk 语言编写脚本
查看>>
在 Linux 上用火狐浏览器保护你的隐私
查看>>
《Hadoop MapReduce性能优化》一1.4 影响MapReduce性能的因素
查看>>
阿里云容器服务-高可用Kubernetes部署指南
查看>>
Make 命令教程
查看>>
Storm-源码分析-Stats (backtype.storm.stats)
查看>>
Java FP: 伪造闭包工厂,创建域对象
查看>>
《Linux C编程从入门到精通》一2.3 Linux中的代码编辑器vim
查看>>
《Redis入门指南》一5.1 PHP与Redis
查看>>
《Hack与HHVM权威指南》——1.6.2 未决的类型
查看>>