In spring we have two types of injections. They are:
In case of Setter method injection, setter method is responsible to perform dependency injection. All the
topics that were explained previously used setter method injection.
In case of constructor injection, we have to define constructors to perform the dependency injection. The following
is an example of constructor injection.
- Setter method injection
- Construcotr injection
First create an Address bean under src as shown below.
File: com.ram.app.Address.java
package com.ram.app; public class Address { private String street; private String city; private String state; public Address(String street, String city, String state) { this.street = street; this.city = city; this.state = state; } public Address(String city, String state) { this.city = city; this.state = state; } public void display(){ System.out.println("Street = "+street); System.out.println("City = "+city); System.out.println("State = "+state); } }
Next create applicationContext.xml file under src. The file is given below.
<?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.0.xsd"> <bean id="addr" class="com.ram.app.Address"> <constructor-arg index="0" value="Richmond"></constructor-arg> <constructor-arg index="1" value="Melbourne"></constructor-arg> <constructor-arg index="2" value="Victoria"></constructor-arg> </bean> </beans>
Next create a main class under src as shown.
File: com.ram.app.ConstructorInjectionApp.java
package com.ram.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConstructorInjectionApp { public static void main(String[] args) { ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml"); Address addr = container.getBean("addr", Address.class); addr.display(); } }
Question: In the Address.java class we have defined two contructors. How will spring know
which constructor has to be executed?
Answer: It is based on the number of "constructor-arg" tag. If we supply three tags, spring container will call three parameter constructor. It is not mandatory that we have to supply "type" and "index" in applicationContext.xml file.
Answer: It is based on the number of "constructor-arg" tag. If we supply three tags, spring container will call three parameter constructor. It is not mandatory that we have to supply "type" and "index" in applicationContext.xml file.
If we do not specify, the first tag is considered as index 0.
Instead of index as the attribute, spring supports "name" attribute. The advantage of using name attribute is, we can know to which parameter the value has been supplied. For example:
<bean id="addr" class="com.ram.app.Address"> <constructor-arg name="street" value="Richmond"></constructor-arg> <constructor-arg name="city" value="Melbourne"></constructor-arg> <constructor-arg name="state" value="Victoria"></constructor-arg> </bean> </beans>
No comments:
Post a Comment