19 August 2017

Dependency Injection with Constructor

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 AnyLanguage(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">
       <constructor-arg   ref="englishHello"/>

    </bean>
    <bean id="spanishHello" class="become.java9pro.springDI.Spanish"/>
    <bean id="englishHello" class="become.java9pro.springDI.English"/>

 </beans>
Here ,we have defined constructor of anyLanguageHello bean as englishHello.
So when container creates anyLanguageHello bean,it will set 'language' to englishHello bean.
So when constructor of AnyLanguage class is invoked,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">     
  <constructor-arg   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.
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee"                   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


</web-app>