Constructor Dependency Injection
Types of Dependency Injection based on how we inject the dependent beans
1. Constructor DI
2. Setter DI
Constructor Dependency Injection ?
Constructor DI is the method of injecting dependent beans through constructor
It means, we pass all the dependent objects as parameters to the constructor and wire them accordingly in the spring configuration file.
We can also inject primitive values through constructor.
Let us see the same with below example
We have Person and Country classes where each person will have an associated country.
So Person is dependent on Country.
Create Person.java
- package com.kb.di;
- public class Person {
- private int id;
- private String name;
- private int age;
- private Country country;
- public int getId() {
- return id;
- }
- public Person(int id, String name, int age, Country country) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.country = country;
- }
- @Override
- public String toString() {
- return "Person details -> id "+id+" name "+name+" age "+age;
- }
- public void showPerson(){
- System.out.println(toString());
- System.out.println(country.toString());
- }
- }
package com.kb.di; public class Person { private int id; private String name; private int age; private Country country; public int getId() { return id; } public Person(int id, String name, int age, Country country) { super(); this.id = id; this.name = name; this.age = age; this.country = country; } @Override public String toString() { return "Person details -> id "+id+" name "+name+" age "+age; } public void showPerson(){ System.out.println(toString()); System.out.println(country.toString()); } }
It contains 3 primitive attributes and 1 dependent object reference to Country.
Person class has a constructor which takes all the 4 parameters and these parameters are injected in the spring configuration file
There is also a toString() method which returns the values of Person
Create Country.java
- package com.kb.di;
- public class Country {
- private int id;
- private String name;
- public Country(int id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- @Override
- public String toString() {
- return "Country details -> id "+id+" name "+name;
- }
- }
package com.kb.di; public class Country { private int id; private String name; public Country(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Country details -> id "+id+" name "+name; } }
It contains 2 primitive attributes
Country class has a constructor which takes all the 2 parameters and these parameters are injected in the spring configuration file
There is also a toString() method which returns the values of Country
Create Spring configuration file(xml file)
- <?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.2.xsd">
- <bean id="country" class="com.kb.di.Country">
- <constructor-arg value="1" type="int"></constructor-arg>
- <constructor-arg value="India"></constructor-arg>
- </bean>
- <bean id="person" class="com.kb.di.Person">
- <constructor-arg value="1" type="int"></constructor-arg>
- <constructor-arg value="Raj"></constructor-arg>
- <constructor-arg value="22" type="int"></constructor-arg>
- <constructor-arg>
- <ref bean="country"/>
- </constructor-arg>
- </bean>
- </beans>
<?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.2.xsd"> <bean id="country" class="com.kb.di.Country"> <constructor-arg value="1" type="int"></constructor-arg> <constructor-arg value="India"></constructor-arg> </bean> <bean id="person" class="com.kb.di.Person"> <constructor-arg value="1" type="int"></constructor-arg> <constructor-arg value="Raj"></constructor-arg> <constructor-arg value="22" type="int"></constructor-arg> <constructor-arg> <ref bean="country"/> </constructor-arg> </bean> </beans>
< constructor-arg > tag is used to inject the values through constructor
value attribute inside < constructor-arg > tag is used to specify the value.
type attribute inside < constructor-arg > tag is used to specify the data type of value we are passing, it should match with the type defined in java class
default type attribute in spring is ‘String’,so we have not specified type for all such string values.
ref is used to specify the reference to another bean with its id.
In this case, it is referring to country.
Pom.xml file
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>Spring</groupId>
- <artifactId>SpringCore</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>SpringCore</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Spring</groupId> <artifactId>SpringCore</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCore</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Spring core related dependencies are specified in this pom.xml
Create Main class(ConstructorDI)
- package com.kb.di;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- public class ConstructorDI {
- public static void main(String[] args) {
- Resource resource=new ClassPathResource("beans.xml");
- BeanFactory beanFactory=new XmlBeanFactory(resource);
- Person person = (Person)beanFactory.getBean("person");
- person.showPerson();
- }
- }
package com.kb.di; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ConstructorDI { public static void main(String[] args) { Resource resource=new ClassPathResource("beans.xml"); BeanFactory beanFactory=new XmlBeanFactory(resource); Person person = (Person)beanFactory.getBean("person"); person.showPerson(); } }
Now right click on ConstructorDI class and run as java application
Output