..


赞助商链接

核心容器 - 构造依赖注入

现在让我们看看,以身作则,如何使用构造依赖注入
我们创造我们的bean上通用的服务而定:






 it.mrwebmaster.di.constructor包;









公共类豆{





  



私人GenericService genericService;



	

  



私人字符串beanName;





  



 / **



   



 *制造商



   



 * @参数genericService



   



 * @参数beanName



   



 * /



  



公共豆(GenericService genericService,弦乐beanName){



    



超级();



    



 this.genericService = genericService;



    



 this.beanName = beanName;



  



 }



	

  



 / **



   



 *公共方法



   



 * /



  



公共无效doit(){



    



 System.out.println(beanName +“做什么”);



    



 genericService.dosomething();



  



 }



	





 }



配置依赖nell'applicationContext。XML





 <bean id="genericService" class="it.mrwebmaster.di.constructor.GenericServiceImpl" scope="singleton" />









 <bean id="constructorBean" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="beanName"/>



  



 <constructor-arg ref="genericService"/>







 </豆>



可以看出,从这个例子使用依赖注入的构造必须使用标记参数的构造函数使用ref或价值的属性。 裁判Attibassi需要传递已经实例化nell'IoC另一个bean容器参数,而价值属性是用来传递字符串或数字的默认值。

在这个例子中类的构造函数接受一个输入GenericService豆类和一个String,但如他想扭转参数的顺序显示Spring如何结合他们的类型参数。
这种行为是好的,当所有参数的不同类型,但我们如何指定的顺序,如果参数是相同类型的? 该标签参数的构造提供了索引属性,表示在春天才能与它传递参数。






 <bean id="constructorBean" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="beanName" index="1" />



  



 <constructor-arg ref="genericService" index="0" />







 </豆>



另一种情况可能是模糊的construttore在其中两个参数作为输入,都可以由一个字符串表示,例如,接受:






公共豆(GenericService genericService,弦乐beanName,invocationTimes整数){



  



超级();



  



 this.genericService = genericService;



  



 this.beanName = beanName;



  



 this.invocationTimes = invocationTimes;







 }



和nell'applicationContext。XML





 <bean id="constructorBean2" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="0" />



  



 <constructor-arg value="beanName" />



  



 <constructor-arg ref="genericService" />







 </豆>



在这种情况下,从为“0”的错误春天可以是字符串或数字。 要解决此问题,使用索引或attriburo像这种类型的属性:





 <bean id="constructorBean2" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg type="java.lang.Integer" value="0" />



  



 <constructor-arg value="beanName" type="java.lang.String" />



  



 <constructor-arg ref="genericService" />







 </豆>



属性构造器参数也可以用来将参数传递给工厂方法:






公共静态豆createBean(GenericService genericService,弦乐beanName,invocationTimes整数){



  



豆B =新豆(genericService,beanName,invocationTimes);



  



 / /不要somethig .......



  



返回B;







 }



nell'applicationContext。XML





 <bean id="constructorBean3" class="it.mrwebmaster.di.constructor.bean" factory-method="createBean">



  



 <constructor-arg type="java.lang.Integer" value="0" />



  



 <constructor-arg value="beanName" type="java.lang.String" />



  



 <constructor-arg ref="genericService" />







 </豆>



春天的Java指南
电子学习
Linux操作系统(课程) Linux操作系统(课程)
完整指南开源系统。 从49€。
PHP(课程) PHP(课程)
全部课程用于创建动态Web站点。 从49€。
Ruby和Ruby on Rails的(课程) Ruby和Ruby on Rails的(课程)
创建Ruby和RoR的软件和Web应用程序。 从39€。
赞助商链接