Spring 中bean 的实例化方法

 bean 的实例化方法主要分三种1构造器(包含调用无参和有参的)2静态工厂方法3实例化工厂方法 


1.普通的通过构造函数初始化,没有指定构造函数参数的就是用默认的无参的构造方法

[java]  view plain  copy
  1. <bean id="exampleBean" class="examples.ExampleBean"/>  
  2.   
  3. <bean name="anotherExample" class="examples.ExampleBeanTwo"/>  


构造函数的几种方式:

1.普通沟通函数注入方式,按照构造函数参数的顺序和个数来注入bean

  

[java]  view plain  copy
  1. package x.y;  
  2.   
  3. public class Foo {  
  4.   
  5.   public Foo(Bar bar, Baz baz) {  
  6.       // ...  
  7.   }  
  8. }  
[java]  view plain  copy
  1. <beans>  
  2.   <bean id="foo" class="x.y.Foo">  
  3.       <constructor-arg ref="bar"/>  
  4.       <constructor-arg ref="baz"/>  
  5.   </bean>  
  6.   
  7.   <bean id="bar" class="x.y.Bar"/>  
  8.   <bean id="baz" class="x.y.Baz"/>  
  9.   
  10. </beans>  


下面几种注入方式先看下示例:

[java]  view plain  copy
  1. package examples;  
  2.   
  3. public class ExampleBean {  
  4.   
  5.   // No. of years to the calculate the Ultimate Answer  
  6.   private int years;  
  7.   
  8.   // The Answer to Life, the Universe, and Everything  
  9.   private String ultimateAnswer;  
  10.   
  11.   public ExampleBean(int years, String ultimateAnswer) {  
  12.       this.years = years;  
  13.       this.ultimateAnswer = ultimateAnswer;  
  14.   }  
  15. }  

2.按照构造函数的参数类型匹配注入

  

[java]  view plain  copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg type="int" value="7500000"/>  
  3. <constructor-arg type="java.lang.String" value="42"/>  
  4. </bean>  

3.按照参数索引顺序注入

  

[java]  view plain  copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg index="0" value="7500000"/>  
  3. <constructor-arg index="1" value="42"/>  
  4. </bean>  

4. spring3以上还可以通过参数名称进行注入

  

[java]  view plain  copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg name="years" value="7500000"/>  
  3. <constructor-arg name="ultimateanswer" value="42"/>  
  4. </bean>  

5.spring3以上通过annotation注入  @ConstructorProperties  
[java]  view plain  copy
  1. package examples;  
  2.   
  3. public class ExampleBean {  
  4.   
  5.   // Fields omitted  
  6.   
  7.   @ConstructorProperties({"years""ultimateAnswer"})  
  8.   public ExampleBean(int years, String ultimateAnswer) {  
  9.       this.years = years;  
  10.       this.ultimateAnswer = ultimateAnswer;  
  11.   }  
  12. }  

6.spring3.1以上还可以使用简化的c namespace来进行构造函数注入

[java]  view plain  copy
  1. <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com">  

c:_index方式注入


[java]  view plain  copy
  1. <-- 'c-namespace' index declaration -->  
  2. <bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz">  



2.通过静态的工厂方法生成bean,这种方式在配置文件中没有指定返回的bean的类型

  

[java]  view plain  copy
  1. <bean id="clientService"  
  2.       class="examples.ClientService"  
  3.       factory-method="createInstance"/>  
[java]  view plain  copy
  1. public class ClientService {  
  2.   private static ClientService clientService = new ClientService();  
  3.   private ClientService() {}  
  4.   
  5.   public static ClientService createInstance() {  
  6.     return clientService;  
  7.   }  
  8. }  

3.通过实例化的工厂方法生成bean

[java]  view plain  copy
  1. <!-- the factory bean, which contains a method called createInstance() -->  
  2. <bean id="serviceLocator" class="examples.DefaultServiceLocator">  
  3.   <!-- inject any dependencies required by this locator bean -->  
  4. </bean>  
  5.   
  6. <!-- the bean to be created via the factory bean -->  
  7. <bean id="clientService"  
  8.       factory-bean="serviceLocator"  
  9.       factory-method="createClientServiceInstance"/>  

[java]  view plain  copy
  1. public class DefaultServiceLocator {  
  2.   private static ClientService clientService = new ClientServiceImpl();  
  3.   private DefaultServiceLocator() {}  
  4.   
  5.   public ClientService createClientServiceInstance() {  
  6.     return clientService;  
  7.   }  
  8. }  

当然这个实例化的工厂类也可以生成多个bean

[java]  view plain  copy
  1. <bean id="serviceLocator" class="examples.DefaultServiceLocator">  
  2.   <!-- inject any dependencies required by this locator bean -->  
  3. </bean>  
  4. <bean id="clientService"  
  5.       factory-bean="serviceLocator"  
  6.       factory-method="createClientServiceInstance"/>  
  7.   
  8. <bean id="accountService"  
  9.       factory-bean="serviceLocator"  
  10.       factory-method="createAccountServiceInstance"/>  

[java]  view plain  copy
  1. public class DefaultServiceLocator {  
  2.   private static ClientService clientService = new ClientServiceImpl();  
  3.   private static AccountService accountService = new AccountServiceImpl();  
  4.   
  5.   private DefaultServiceLocator() {}  
  6.   
  7.   public ClientService createClientServiceInstance() {  
  8.     return clientService;  
  9.   }  
  10.   
  11.   public AccountService createAccountServiceInstance() {  
  12.     return accountService;  
  13.   }  
  14. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值