Wednesday 10 November 2021

Spring: inject dependencies to a self-instantiated object

In this post, I am going to explain how to inject dependencies to a self-instantiated object.

 

Using 'AutowireCapableBeanFactory' interface, we can inject dependencies to a self-instantiated object.

 

Step 1: Get an instance of 'AutowireCapableBeanFactory'.

@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;

Step 2: Inject dependencies using autowireBean method.

ListUtil listUtil = new ListUtil();
autowireCapableBeanFactory.autowireBean(listUtil);


‘autowireBean’ method is used to (re-)populate the annotated fields and methods, either for new instances or for deserialized instances.

 

Find the below working application.

 

Step 1: Create new maven project ‘inject-dependencies-to-self-instantiated-object’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>inject-dependencies-to-self-instantiated-object</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.0</version>

	</parent>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

</project>


Step 3: Define the components.

 

StringUtil.java

package com.sample.app.components;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class StringUtil {

	public List<String> toUpper(List<String> strs) {
		if (strs == null || strs.isEmpty()) {
			return Collections.EMPTY_LIST;
		}

		List<String> uppercaseStrings = new ArrayList<>();
		for (String str : strs) {
			uppercaseStrings.add(str.toUpperCase());
		}
		return uppercaseStrings;

	}

}


ListUtil.java

package com.sample.app.components;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ListUtil {

	@Autowired
	private StringUtil stringUtil;

	public List<String> toUpper(List<String> strs) {
		return stringUtil.toUpper(strs);

	}
}


Step 4: Define main application class.

 

App.java

package com.sample.app;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.components.ListUtil;

@SpringBootApplication
public class App {
	
	@Autowired
	private AutowireCapableBeanFactory autowireCapableBeanFactory;

	public static void main(String args[]) {
		SpringApplication.run(App.class, args);
	}

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {

			ListUtil listUtil = new ListUtil();
			autowireCapableBeanFactory.autowireBean(listUtil);

			List<String> uppercaseStrings = listUtil.toUpper(Arrays.asList("One", "two", "three"));
			uppercaseStrings.stream().forEach(System.out::println);

		};
	}

}


Total project structure looks like below.





Run App.java, you will see below messages in console.


ONE
TWO
THREE


You can download the complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/miscellaneous/inject-dependencies-to-self-instantiated-object

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment