Spring中bean的范围

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
     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 custA
Message : Message by custA
2. Prototype example
1
2
3
4
5
6
7
8
9
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
    < bean  id="customerService" class="com.mkyong.customer.services.CustomerService"
          scope="prototype"/>
  
</ beans >

  运行输出为:

Message : Message by custA
Message : 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
     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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值