Spring Boot Dependency Injection and Application Runner

Spring Boot Dependency Injection and Application Runner

In this article, I am going to discuss Spring Boot Dependency Injection and Application Runner. Please read our previous article where we discussed Spring Boot Tomcat Deployment.

Spring Boot Dependency Injection

As we know by now, Spring Boot is an extension of the Spring Framework. So, we can use Spring Framework features such as beans and dependency injection with more ease. Dependency injection means the Spring container injects objects into other objects. This helps to make code more loosely coupled. Furthermore, this also makes the container responsible for the management of components.

@ComponentScan annotation is used to find the beans. When using Spring Framework, @ComponentScan annotation needs to be provided with arguments (regarding the beans). However, with the Spring Boot layout, @ComponentScan does not need to be provided with any arguments. All components are automatically registered with Spring Boot.

For dependency injection, @Autowired annotation is used. To use this feature, follow the steps below:

Step 1: Initialise the Spring project using Spring Initializr (in VS Code). Find the steps for this in our previous article (Spring Boot Setup).

Step 2: Add spring-boot-starter-web dependency in the pom.xml file.

Spring Boot Dependency Injection

Step 3: Modify code in src/main/java/BeansApplication.java as below:

  • Import 3 packages
  • Create an Autowired global variable of type RestTemplate
  • Create a function ‘Bean’. This function is initializing Autowired global variable, mentioned above

After modifications, our code should look like this:

Spring Boot Dependency Injection

Step 4: Run the application using the Run button on the top right of the window. The following output indicates that the code is working correctly.

Spring Boot Dependency Injection

However, if you go to http://localhost:8080, no output will be displayed. This is because we have not implemented a REST controller yet.

Spring Boot Dependency Injection

Spring Boot Application Runner

Application Runner is a feature of Spring Boot that allows for code to be executed after the application has started. To use this feature, follow the steps below:

Step 1: Create a new project using Spring Initializr in VS Code.

Step 2: Modify code in the src/main/java/ApplicationrunnerApplication.java file as follows:

  • Import Two packages ApplicationArguments and ApplicationRunner
  • Add ‘implements ApplicationRunner’ to the class
  • Add a run() function so that it overrides the function in the ApplicationRunner interface. Code written in this function shall execute after the application starts running

After modifications, our code looks like this:

Spring Boot Application Runner

Step 3: Run the application. The following output should be displayed:

Spring Boot Application Runner

As can be seen, the print statements execute after the application starts running.

Congratulations! You have successfully learned how to perform dependency injection and implement runners in Spring Boot.

In the next article, I am going to discuss Spring Boot Application Properties. Here, in this article, I try to explain Spring Boot Dependency Injection and Application Runner. I hope you enjoy this Spring Boot Dependency Injection and Application Runner article.

Leave a Reply

Your email address will not be published. Required fields are marked *