In this tutorial you will learn about the Constructor-based dependency injection in spring framework.
In this tutorial you will learn about the Constructor-based dependency injection in spring framework.In the constructor-based dependency injection one bean definition is injected to another. For
this you use the constructor-arg or property's ref attribute
instead of the value attribute.
In this example, you will see a bean definition is injected to another bean. The
first bean definition is the FirstBean with the id firstBean. It is
injected into the another bean definition by reference using the property
element's ref attribute.
FirstBean.java
package net.roseindia;
|
AnotherBean.java
package net.roseindia;
|
ReferenceMain.java
package net.roseindia; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ReferenceMain { public static void main(String[] args) { BeanFactory beanfactory = new ClassPathXmlApplicationContext( "context.xml"); FirstBean bean = (FirstBean) beanfactory.getBean("show"); bean.display("hello spring"); } } |
context.xml
<?xml version="1.0" encoding="UTF-8"?>
|
Hello
Spring