Spring进阶之路(5)-Spring创建Bean的三种方式

Spring可以通过调用构造器来创建Bean、调用静态工厂方法创建Bean、调用实例工厂方法创建Bean。



构造器创建Bean实例


之前几篇文章中已经讲述了构造注入的实例,这里略微阐述一下,构造器创建Bean,如果配置文件中配置了构造注入的方式的话,就会根据配置文件中配置的方式获取构造方法,然后创建实例,如果没有进行配置那么Spring会直接调用无参的构造方法创建该类的实例。



静态工厂的方法创建Bean实例


定义一个Person接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301;  
  2.   
  3. public interface Person {  
  4.   
  5.     public void saySth();  
  6. }  


一个WangYang类和WY类分别实现这个Person接口并且实现他的接口。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301;  
  2.   
  3. public class WangYang implements Person{  
  4.   
  5.     private String descrip;  
  6.       
  7.     public String getDescrip() {  
  8.         return descrip;  
  9.     }  
  10.     public void setDescrip(String descrip) {  
  11.         this.descrip = descrip;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void saySth() {  
  16.         System.out.println(this.descrip + "wangyang");  
  17.     }  
  18.   
  19.       
  20. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301;  
  2.   
  3. public class WY implements Person{  
  4.   
  5.     private String descrip;  
  6.       
  7.     public String getDescrip() {  
  8.         return descrip;  
  9.     }  
  10.     public void setDescrip(String descrip) {  
  11.         this.descrip = descrip;  
  12.     }  
  13.     @Override  
  14.     public void saySth() {  
  15.         System.out.println(this.descrip + "wy");  
  16.     }  
  17.   
  18. }  

BeanFactory类,是一个静态工厂类,该类的getBean要是一个静态方法,不然的话会抛出异常,通过这个getBean方法获得对应的实例。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301;  
  2.   
  3. public class BeanFactory {  
  4.   
  5.     public static Person getBean(String arg){  
  6.           
  7.         if("wangyang".equals(arg)){  
  8.             return new WangYang();  
  9.         }else if("wy".equals(arg)){  
  10.             return new WY();  
  11.         }  
  12.         return null;  
  13.     }  
  14. }  

接下来在配置文件中,下面的constructor-arg标签中的值为上面的静态工厂类的getBean方法传入参数arg. Spring利用反射获得bean:wangyang = BeanFactory.getBean("wangyang"); 这样就将WangYang这个类实例化了。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.   
  7. <!-- bean definitions here -->  
  8.         <bean id = "wangyang" class = "com.siti.spring20160301.BeanFactory" factory-method="getBean">  
  9.             <constructor-arg value="wangyang"></constructor-arg>  
  10.             <property name="descrip" value="powerful"></property>  
  11.         </bean>  
  12.         <bean id = "wy" class = "com.siti.spring20160301.BeanFactory" factory-method="getBean">  
  13.             <constructor-arg value="wy"></constructor-arg>  
  14.             <property name="descrip" value="hard work"></property>  
  15.         </bean>  
  16. </beans>  




实例工厂方法创建Bean


实例工厂方法需要创建一个工厂的实例,然后由工厂实例去产生其他的实例对象。基本的实例如下所示,大部分的代码与上面的静态工厂类方法相同,只是略微改了一点地方而已。


定义Person接口,定义一个方法:saySth

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301shilifactory;  
  2.   
  3. public interface Person {  
  4.   
  5.     public void saySth();  
  6. }  

WangYang和WY分别实现这个接口。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301shilifactory;  
  2.   
  3. public class WangYang implements Person{  
  4.   
  5.     private String descrip;  
  6.       
  7.     public String getDescrip() {  
  8.         return descrip;  
  9.     }  
  10.     public void setDescrip(String descrip) {  
  11.         this.descrip = descrip;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void saySth() {  
  16.         System.out.println(this.descrip + "wangyang");  
  17.     }  
  18.   
  19.       
  20. }  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301shilifactory;  
  2.   
  3. public class WY implements Person{  
  4.   
  5.     private String descrip;  
  6.       
  7.     public String getDescrip() {  
  8.         return descrip;  
  9.     }  
  10.     public void setDescrip(String descrip) {  
  11.         this.descrip = descrip;  
  12.     }  
  13.     @Override  
  14.     public void saySth() {  
  15.         System.out.println(this.descrip + "wy");  
  16.     }  
  17.   
  18. }  


BeanFactory如下,需要创建实例,Spring用无参的构造方法创建其实例。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160301shilifactory;  
  2.   
  3. public class BeanFactory {  
  4.   
  5.     public Person getBean(String arg){  
  6.           
  7.         if("wangyang".equals(arg)){  
  8.             return new WangYang();  
  9.         }else if("wy".equals(arg)){  
  10.             return new WY();  
  11.         }  
  12.         return null;  
  13.     }  
  14. }  

配置文件:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.   
  7.         <!-- 实例工厂 -->  
  8.         <bean id = "factory" class = "com.siti.spring20160301shilifactory.BeanFactory"></bean>  
  9.         <bean id = "wangyang" factory-bean="factory" factory-method="getBean">  
  10.             <constructor-arg value="wangyang"></constructor-arg>  
  11.             <property name="descrip" value="powerful - "></property>  
  12.         </bean>  
  13.         <bean id = "wy" factory-bean="factory" factory-method="getBean">  
  14.             <constructor-arg value="wy"></constructor-arg>  
  15.             <property name="descrip" value="hard work - "></property>  
  16.         </bean>  
  17. </beans>  

静态工厂方法不需要实例化BeanFactory,但是实例工厂方法需要实例化BeanFactory。在配置文件中静态工厂是通过class来定位Factory而实例工厂则是通过factory-bean来定位Factory的。

但是他们都是通过factory-method来指定调用的方法,都是通过constructor-arg标签来指定前面调用的方法的那个参数,也都是通过property来为创建的实例对象注入属性值的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值