..
Use the Constructor Dependency Injection can lead to the creation of cyclic dependencies between beans. For example, if an outgoing subject headers need to be instantiated to an object B, but at the same time the object A object B needs, it creates a dependency that can not be resolved using the Constructor Dependency Injection.
In such cases you must use Setter Dependency Injection is very similar to the Constructor but the difference lies in the fact that dependencies are injected after the object was instantiated. Here's an example:
{public class Bean2
Private GenericService genericService;
public void setGenericService (GenericService genericService) {
this.genericService = genericService;
}
public GenericService getGenericService () {
genericService return;
}
/ **
* Business logic .............
* /
}
nell'applicationContext. xml
<bean id="setterBean" class="it.mrwebmaster.di.setter.Bean2"> <property name="genericService" ref="genericService" /> </ Bean>
In addition to solving the problem of the circular dipenze Setter Dependency Injection can also be reconfigured at runtime the beans, for these reasons it is preferred to the Constructor. Some schools of thought, however, say that the good would be to use entrabe: Constructor for the necessary dependencies and setters for those options. It is advisable to use the setter as it is very rare that there are optional dependencies.
Besides the basic use of DI, there are other features that Spring offers, such as empty strings, or you can inject null fields:
<! - NULL VALUE 1 -> <bean id="nullBean1" class="it.mrwebmaster.di.constructor.bean" factory-method="createBean"> <constructor-arg type="java.lang.Integer" value="0" /> <constructor-arg type="java.lang.String" value="" /> <constructor-arg type="it.mrwebmaster.di.constructor.GenericService"> <null/> </ constructor-arg> </ Bean> <! - 2 VALUE NULL -> <bean id="nullBean2" class="it.mrwebmaster.di.setter.Bean2"> <property name="genericService"> <null/> </ property> </ Bean>
Spring also offers a collection of management type List, Set, Map, Properties, and through the tag list, set, map, and props:
{public class CollectionBean
private List <GenericService> beanList;
Private Properties beanProps;
Private <String> beanSet September;
private Map <Integer, string> beanMap;
/ **
* Business logic ......................
* /
<GenericService> getBeanList public List () {
beanList return;
}
public Map <Integer, string> getBeanMap () {
beanMap return;
}
Public Properties getBeanProps () {
beanProps return;
}
<String> getBeanSet public Set () {
beanSet return;
}
public void setBeanList (List <GenericService> beanList) {
this.beanList = beanList;
}
public void setBeanMap (Map <Integer, string> beanMap) {
this.beanMap = beanMap;
}
public void setBeanProps (Properties beanProps) {
this.beanProps = beanProps;
}
public void setBeanSet (Set <String> beanSet) {
this.beanSet = beanSet;
}
}
nell'applicationContext. xml
<! - BEAN COLLECTION ->
<bean id="collectionBean" class="it.mrwebmaster.di.collection.CollectionBean">
<! - LIST ->
<property name="beanList">
<list>
<ref bean="genericService"/>
</ List>
</ Property>
<! - PROPERTIES ->
<property name="beanProps">
<props>
<prop key="prop1"> value1 </ prop>
<prop key="prop2"> value2 </ prop>
<prop key="prop3"> value3 </ prop>
</ Props>
</ Property>
<! - SET ->
<property name="beanSet">
<set>
<value> to </ value>
<value> b </ value>
</ Set>
</ Property>
<! - MAP ->
<property name="beanMap">
<map>
<entry key="1" value="value1" />
</ Map>
</ Property>
</ Bean>
It leaves the reader with the depth on other issues such as the collection of the merge, the inner beans and collaborators.
| |
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 €. |