Monday, July 29, 2013

Spring Inversion of Control

Bean Creation
- code POJO, add bean configuration element to the Spring XML configuration file or annotate the POJO
<bean id="message" class="org.springbyexample.di.xml.BelleMessage"/>

Inversion of Control
1. Constructor Injection

public ConstructorMessage(String message) {
    this.message = message;
}

<bean id="message" class="org.springbyexample.di.xml.ConstructorMessage">
    <constructor-arg value="Spring is fun." />
</bean>

2. Setter Injection

public void setMessage(String message) {
    this.message = message;
}

<bean id="message" class="org.springbyexample.di.xml.SetterMessage">
    <property name="message" value="Spring is fun." />
</bean>

3. Reference Injection

<bean id="message" class="org.springbyexample.di.xml.SetterMessage">
    <property name="message" ref="springMessage"" />
</bean>


source: http://www.springbyexample.org/examples/intro-to-ioc.html

No comments:

Post a Comment