Quote

We are what we repeatedly do. Excellence, therefore,is not an act but a habit.

Aristotle

Wednesday, January 6, 2010

Setter Injection DI

Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
____________________________________________________________________________________



public class ExampleBean {

private AnotherBean beanOne;

private YetAnotherBean beanTwo;

private int i;

public void setBeanOne(AnotherBean beanOne) {

this.beanOne = beanOne;

}

public void setBeanTwo(YetAnotherBean beanTwo) {

this.beanTwo = beanTwo;

}

public void setIntegerProperty(int i) {

this.i = i;

}

}



______________________________________________________________________________



<bean id="exampleBean" class="examples.ExampleBean">

<!-- setter injection using the nested <ref/> element -->

<property name="beanOne"><ref bean="anotherExampleBean"/></property>

<!-- setter injection using the neater 'ref' attribute -->

<property name="beanTwo" ref="yetAnotherBean"/>

<property name="integerProperty" value="1"/>

</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>

<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>


_____________________________________________________________________________________

No comments:

Post a Comment