..
Now let's see, by example, how to use the Constructor Dependency Injection
We create our bean that depends on a generic service:
it.mrwebmaster.di.constructor package;
public class Bean {
Private GenericService genericService;
private String beanName;
/ **
* Manufacturer
* @ Param genericService
* @ Param beanName
* /
public Bean (GenericService genericService, String beanName) {
super ();
this.genericService = genericService;
this.beanName = beanName;
}
/ **
* Public method
* /
public void doit () {
System.out.println (beanName + "doing something");
genericService.dosomething ();
}
}
configure dependency 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"/> </ Bean>
As can be seen from the example to use the Constructor Dependency Injection must use the tag-arg constructor using the ref or value attributes. The ref Attibassi need to pass as arg already instantiated nell'IoC another bean container, while the value attribute is used to pass default values as strings or numbers.
In the example the class constructor accepts as input a GenericService beans and a String, but the example he wanted to reverse the order of arguments to show how Spring combining the arguments by their type.
This type of behavior is fine when all arguments are of different types, but how do we specify the order if the arguments are the same type?
The tag-arg constructor provides the index attribute that indicates the order in Spring with which to pass parameters.
<bean id="constructorBean" class="it.mrwebmaster.di.constructor.bean"> <constructor-arg value="beanName" index="1" /> <constructor-arg ref="genericService" index="0" /> </ Bean>
Another case might be an ambiguous one in which the construttore accepts as input two parameters that can both be represented by a string, for example:
public Bean (GenericService genericService, String beanName, invocationTimes Integer) {
super ();
this.genericService = genericService;
this.beanName = beanName;
this.invocationTimes = invocationTimes;
}
and nell'applicationContext. xml
<bean id="constructorBean2" class="it.mrwebmaster.di.constructor.bean"> <constructor-arg value="0" /> <constructor-arg value="beanName" /> <constructor-arg ref="genericService" /> </ Bean>In this case, spring from error as "0" can be either a string or a number. To resolve this problem, use the index or attriburo the type attribute like this:
<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" /> </ Bean>
The attribute constructor-arg can also be used to pass parameters to the factory method:
public static bean createBean (GenericService genericService, String beanName, invocationTimes Integer) {
Bean b = new Bean (genericService, beanName, invocationTimes);
/ / Do somethig .......
return 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" /> </ Bean>
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |