19 August 2017

Spring Dependency Injection with Setter

Define A interface Language

package become.java9pro.springDI;

public interface Language {

    String sayHello();
}
Define A class English which implements Language
package become.java9pro.springDI;

import org.springframework.stereotype.Component;


public class English implements Language {

    public String sayHello(){
        return "Hello";
    }
}
Define A class Spanish which implements Language
package become.java9pro.springDI;

import org.springframework.stereotype.Component;


public class Spanish implements Language {

    public String sayHello(){
        return "hola";
    }
}
Define  AnyLanguage Class with Language as property
package become.java9pro.springDI;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;


public class AnyLanguage{

    Language language;

    public void setLanguage(Language language) {
        this.language = language;
        System.out.println("******"+language.sayHello());
    }

}

Define bean dependencies with properties in spring context file as below.
<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
xmlns:context="http://www.springframework.org/schema/context"       
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd      
 http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd ">


    <context:component-scan base-package="become.java9pro"/>

    <bean id="anyLanguageHello" class="become.java9pro.springDI.AnyLanguage">        
<property name="language"  ref="englishHello"/>
    </bean>
    <bean id="spanishHello" class="become.java9pro.springDI.Spanish"/>
    <bean id="englishHello" class="become.java9pro.springDI.English"/>

 </beans>
Here ,we have defined language property of anyLanguageHello bean as englishHello.
So when container creates anyLanguageHello bean,it will set the property 'language' to englishHello bean.So when setter of language is invoked in AnyLanguage class,it will execute sayHello method in English class and will output as ******Hello.
<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
xmlns:context="http://www.springframework.org/schema/context"      
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd      
 http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd ">


    <context:component-scan base-package="become.java9pro"/>

    <bean id="anyLanguageHello" class="become.java9pro.springDI.AnyLanguage">        
<property name="language"  ref="spanishHello"/>
    </bean>
    <bean id="spanishHello" class="become.java9pro.springDI.Spanish"/>
    <bean id="englishHello" class="become.java9pro.springDI.English"/>

 </beans>
If we change englishHello to spanishHello,it will execute sayHello method in Spanish class.
it will output as ******hola.