Dependency Inject in springs
Interface.
package depend;
public interface Naveen {
public void printe();
}
package depend;
public interface Naveen {
public void printe();
}
Class
package depend;
public class Jyoti implements Naveen
{
@Override
public void printe() {
System.out.print("Naveen && Jyoti");
}
}
Helper Class
package depend;
public class Output {
Naveen naveen;
public void setNaveen(Naveen naveen) {
this.naveen = naveen;
}
public void printe()
{
naveen.printe();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dao" class="naveen.Dao">
<property name="name" value="Naveen" />
</bean>
<bean id="Output" class="depend.Output">
<property name="naveen" ref="Jyoti" />
</bean>
<bean id="Jyoti" class="depend.Jyoti" />
</beans>
Main-class
package depend;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String args[])
{
ApplicationContext tx=new ClassPathXmlApplicationContext("applicationContext.xml");
Output o=(Output)tx.getBean("Output");
o.printe();
}
}