Dependency Injection Configuration — XML Based
Java Spring
So Why do we need Dependency Injection, what purpose it solves?
- Modularisation of Code ( Loose Coupling )
- Recompilation of Code is not needed, if need to change after deployment ( In XML Based approach )
- Re-usability of Code
- More Testable Code
Types of DI in Java Spring
- Constructor Injection
Let us get our hands dirty!!
<!-- 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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- Define your beans here --> <bean id="Bank1" class="com.springdemo.Bank1"> </bean> <bean id="Bank2" class="com.springdemo.Bank2"> </bean> <bean id="consortium" class="com.springdemo.Consortium"> <!-- Dependency injection --> <constructor-arg index="0">
<list>
<ref bean="Bank1" />
<ref bean="Bank2" />
</list>
</constructor-arg>
<constructor-arg index="1" type = "java.lang.String" value =
"December 2018"/> </bean></beans>
In the above xml, there are 2 reference beans and a String passed as arguments to the Consortium bean.
Below code represents the two Banks implementing the Bank Interface
<!-- Bank1.java -->public class Bank1 implements Bank
{
private String b_name = "Xyz Bank"; @Override
public String getBankName()
{
return b_name;
}
}<!-- Bank2.java -->public class Bank2 implements Bank
{
private String b_name = "Abc Bank"; @Override
public String getBankName()
{
return b_name;
}
}
And here is the Consortium of banks:
<!-- Consortium.java -->public class Consortium
{
private List<Bank> banks;
private String as_of;
public Consortium(List<Bank> banks,String as_of)
{
this.banks = banks;
this.as_of = as_of;
} public void getConsortiumDetails()
{
System.out.println("Consortium Details as of"+ as_of);
for(Bank bank:banks)
{
System.out.println("Name:"+bank.getBankName());
}
}
}
And finally the wrapper Class which would initiate the ApplicationContext.xml
public class MySpringApp { public static void main(String[] args) { ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("ApplicationContext.xml"); Consortium theConsortium =
context.getBean("consortium",Consortium.class); System.out.println(theConsortium.getConsortiumDetails()); context.close(); }}
And the Expected Output
Consortium Details as of December 2018:
Name: Xyz Bank
Name: Abc Bank
2. Property Setter Injection
Let us see the above example modified to demonstrate Property setter injection
<!-- 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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- Define your beans here --><bean id="Bank1" class="com.springdemo.Bank1"></bean><bean id="Bank2" class="com.springdemo.Bank2"></bean><bean id="consortium" class="com.springdemo.Consortium"><!-- Dependency injection --> <property name="Banks">
<list>
<ref bean="Bank1" />
<ref bean="Bank2" />
</list>
</property>
<property name="As_of" value="December 2018"/></bean></beans>
Below code represents the two Banks implementing the Bank Interface
<!-- Bank1.java -->public class Bank1 implements Bank
{
private String b_name = "Xyz Bank"; @Override
public String getBankName()
{
return b_name;
}
}<!-- Bank2.java -->public class Bank2 implements Bank
{
private String b_name = "Abc Bank"; @Override
public String getBankName()
{
return b_name;
}
}
And here is the Consortium of banks modified to suit Property Injection:
<!-- Consortium.java -->public class Consortium
{
private List<Bank> banks;
private String as_of;
public setBanks(List<Bank> banks)
{
this.banks = banks;
}
public setAs_of(String as_of)
{
this.as_of = as_of;
} public getConsortiumDetails()
{
System.out.println("Consortium Details as of"+ as_of);
for(Bank bank:banks)
{
System.out.println("Name:"+bank.getBankName());
}
}
}
And finally the wrapper Class which would initiate the ApplicationContext.xml
public class MySpringApp { public static void main(String[] args) { ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("ApplicationContext.xml"); Consortium theConsortium =
context.getBean("consortium",Consortium.class); System.out.println(theConsortium.getConsortiumDetails()); context.close(); }}
And we get the same Expected Output
Consortium Details as of December 2018:
Name: Xyz Bank
Name: Abc Bank
The Bean and It’s Scope
By Default the scope of the Bean is Singleton [ In object-oriented programming, a singleton class is a class that can have only one object at a time.]
We can define the scope of the Bean through ApplicationContext.xml.
<bean id="consortium" class="com.springdemo.Consortium" scope="prototype" init-method="callOnStart"
destroy-method="callOnEnd"><!-- Dependency injection --> <property name="Banks">
<list>
<ref bean="Bank1" />
<ref bean="Bank2" />
</list>
</property>
<property name="As_of" value="December 2018"/></bean>
In the above snippet, there are two observations
- Scope is defined as Prototype ( That is, it would create a new object for each request. )
The possible scopes to the Bean:
Singleton
Prototype
Request
Session
Global Session
- You would have also noticed the init-method and the destroy-method being used.
Init-method is called when the Bean is formed and the Destroy-method is called on death of the Bean object.
Note: Prototype Bean doesn’t call the Destroy method. The Init and Destroy methods are supposed to be No-Arg methods.
Happy Coding!!
If you are looking for an easier way to inject dependency without depending much on ApplicationContext.xml, my next blog would be helpful to you
Please feel free to correct or leave your suggestions in comments below