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

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

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>

转载于:https://www.cnblogs.com/youngdream-ppj/archive/2013/04/02/2996708.html

你可能感兴趣的文章
40个Java多线程问题总结
查看>>
DBImport v3.5 中文版发布:数据库定时同步及文档生成工具(IT人员必备)
查看>>
020-添加用户
查看>>
023-手动增加swap分区
查看>>
20.27 分发系统介绍
查看>>
Java进阶(一)使用new Date()和System.currentTimeMillis()获取当前时间戳
查看>>
推荐引擎
查看>>
Java字符串常亮池
查看>>
如何学习区块链
查看>>
华为加班有多可怕?
查看>>
灵活强大的MySQL代理中间件ProxySQL应用实战(1)
查看>>
Python项目实战:批量下载高清美图
查看>>
AIDL的实现
查看>>
OSChina 周三乱弹 —— 领导,请尊重我的专业!我不要再搬砖了!!!
查看>>
OSChina 周五乱弹 ——落落酱给你们发中秋礼品啦
查看>>
OSChina 周三乱弹 —— 听说分手以后你到处说我死了
查看>>
jQuery AJAX
查看>>
数据库日志收缩
查看>>
建造者模式——创建型模式
查看>>
Python基础数据类型
查看>>