Dependency injection in Spring

Introduction

Here we will see different types of dependency injections in Spring framework.

There are three types of dependency injections and they are constructor injection, setter injection and interface injection.

Spring supports only constructor and setter injection. Interface injection is supported by other language like Avalon.

Interface injection a different type of DI(dependency injection) that involves mapping items to inject to specific interfaces.

Spring supports Setter Injection via Java bean setters; and Constructor Injection via constructor arguments.

You may find useful tutorial on what is dependency pattern?

Constructor Injection

Object is injected through constructor as an argument.

package com.roytuts.spring.dependency.example;

public class ConstructorInjectionExample {

	private String message;

	/**
	 * Constructor
	 */
	public ConstructorInjectionExample(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

The constructor-arg element injects a message into the bean using the constructor-arg element’s value attribute.

The file beans-constructor.xml is put under classpath src/main/resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="messageCI"
		class="com.roytuts.spring.dependency.example.ConstructorInjectionExample">
		<constructor-arg
			value="Constructor Injection in Spring" />
	</bean>
</beans>

Write main class to get the bean.

package com.roytuts.spring.dependency.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConstructorInjectionExampleTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans-constructor.xml");

		ConstructorInjectionExample constructorInjectionExample = context.getBean(ConstructorInjectionExample.class);

		System.out.println(constructorInjectionExample.getMessage());

		((ConfigurableApplicationContext) context).close();
	}

}

Running the above file will give you the following output:

Constructor Injection in Spring

Annotation Based Constructor Injection

We have seen how to work with constructor based dependency injection in Spring framework.

Let’s say we want to use annotation based configuration instead of XML based configuration.

So here we are going to create a configuration class instead of XML file. The below class defines a bean for ConstructorInjectionExample.

package com.roytuts.spring.dependency.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConstructorConfig {

	@Bean
	public ConstructorInjectionExample example() {
		return new ConstructorInjectionExample("Annotation based constructor injection in Spring");
	}

}

Now we will also write main class with AnnotationConfigApplicationContext to get the bean defined in classpath.

package com.roytuts.spring.dependency.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnotationConstructorInjectionExampleTest {

	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(ConstructorConfig.class);

		ConstructorInjectionExample constructorInjectionExample = context.getBean(ConstructorInjectionExample.class);

		System.out.println(constructorInjectionExample.getMessage());

		((ConfigurableApplicationContext) context).close();
	}

}

Executing the above class gives you the following output:

Annotation based constructor injection in Spring

Setter Injection

The object is injected through setter as an argument.

package com.roytuts.spring.dependency.example;

public class SetterInjectionExample {

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

The property element of <bean/> tag in XML config file is used to define the setter injection.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="messageSI"
		class="com.roytuts.spring.dependency.example.SetterInjectionExample">
		<property name="message" value="Setter Injection in Spring" />
	</bean>
</beans>

Write main class to test it out.

package com.roytuts.spring.dependency.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetterInjectionExampleTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans-setter.xml");

		SetterInjectionExample setterInjectionExample = context.getBean(SetterInjectionExample.class);

		System.out.println(setterInjectionExample.getMessage());

		((ConfigurableApplicationContext) context).close();
	}

}

Executing the above main class will give you the following output:

Setter Injection in Spring

Annotation Based Setter Injection

For setter based dependency injection also we also create a config class.

package com.roytuts.spring.dependency.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SetterConfig {

	@Bean
	public SetterInjectionExample example() {
		SetterInjectionExample example = new SetterInjectionExample();
		example.setMessage("Annotation based setter injection in Spring");

		return example;
	}

}

We need to create a main class to test our application.

package com.roytuts.spring.dependency.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnotationSetterInjectionExampleTest {

	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(SetterConfig.class);

		SetterInjectionExample setterInjectionExample = context.getBean(SetterInjectionExample.class);

		System.out.println(setterInjectionExample.getMessage());

		((ConfigurableApplicationContext) context).close();
	}

}

Executing the above class will give you the following output:

Annotation based setter injection in Spring

Source Code

Download

Thanks for reading.

Leave a Comment