A simple Spring example to show you how to dependency inject a bean via setter method, the most common used DI method.
WildAnimal.java – Interface backbone using which dependency injection is done.
package com.mahesh.ioc;
public interface WildAnimal {
public String sound() ;
}
Lion.java – Implements the above interface and becomes a possible candidate for injection.
package com.mahesh.ioc;
public class Lion implements WildAnimal {
public String sound() {
return "Roar";
}
}
Wolf.java – another implementation of interface.
package com.mahesh.ioc;
public class Wolf implements WildAnimal {
public String sound() {
return "Howl";
}
}
Zoo.java – Zoo depends on WildAnimal and uses setter injection.
package com.mahesh.ioc;
public class Zoo {
private WildAnimal wild;
public void setWild(WildAnimal wild) {
this.wild = wild;
}
public void testSound() {
System.out.println(wild.sound());
}
}
spring-context.xml – gives the configuration information to the spring container. In spring configuration we declare the chosen implementation to supply at runtime.
TestDI.java – loads the spring configuration and gets the bean.
package com.mahesh.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDI {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Zoo zoo = (Zoo) context .getBean("zoo");
zoo.testSound();
}
}
WildAnimal.java – Interface backbone using which dependency injection is done.
package com.mahesh.ioc;
public interface WildAnimal {
public String sound() ;
}
Lion.java – Implements the above interface and becomes a possible candidate for injection.
package com.mahesh.ioc;
public class Lion implements WildAnimal {
public String sound() {
return "Roar";
}
}
Wolf.java – another implementation of interface.
package com.mahesh.ioc;
public class Wolf implements WildAnimal {
public String sound() {
return "Howl";
}
}
Zoo.java – Zoo depends on WildAnimal and uses setter injection.
package com.mahesh.ioc;
public class Zoo {
private WildAnimal wild;
public void setWild(WildAnimal wild) {
this.wild = wild;
}
public void testSound() {
System.out.println(wild.sound());
}
}
spring-context.xml – gives the configuration information to the spring container. In spring configuration we declare the chosen implementation to supply at runtime.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans>
<bean id="wild" class="com.mahesh.ioc.wolf" />
<bean id="zoo" class="com.mahesh.ioc.zoo" /> <property name="wild" ref="wild" /> </bean> </beans>
TestDI.java – loads the spring configuration and gets the bean.
package com.mahesh.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDI {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Zoo zoo = (Zoo) context .getBean("zoo");
zoo.testSound();
}
}
No comments:
Post a Comment