Table of contents
Introduction
In Java, a Spring Bean is an object that is created and managed by Spring IoC container. In Java-based configuration, Beans can be defined using @Configuration and @Bean annotations. There are multiple ways to define and configure beans, as briefly overviewed in Loose Coupling in Spring Boot - PART 2 including XML-based, Annotation-based, and Java-based configurations. In this article, we'll delve into Java-based configuration.
Steps for Configuring Spring Beans
Create classes (Components) that'll be managed as Beans by Spring IoC container. Here, we'll create three java classes namely MyController, MyService, and MyRepository whose objects should be managed using Spring Beans.
package com.mypackage.controller; import com.mypackage.service.MyService; public class MyController { //Controller -> Service Layer -> Data Layer(Repository) private final MyService myService; public MyController(MyService myService) { this.myService = myService; } public String handleRequest() { return myService.serveRequest(); } }
package com.mypackage.service; public class MyService { public String serveRequest() { return "Service Layer"; } }
package com.mypackage.repository; public class MyRepository { public String getData() { return "Data Layer"; } }
Create a Java class and annotate it with "@Configuration". This class will contain methods annotated with "@Bean" to define your Beans.
package com.mypackage.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } @Bean public MyRepository myRepository() { return new MyRepository(); } /*Creating Bean (name = myController, type = MyController) using existing Bean (name = myService, type = MyService)*/ @Bean public MyController myController() { return new MyController(myService()); } }
Ensure that Main application class does the task of launching context.
package com.mypackage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.mypackage.repository.MyRepository; import com.mypackage.config.AppConfig; import com.mypackage.controller.MyController; public class MyApplication { public static void main(String[] args) { //Launch Context with configurtion AppConfig.java var context = new AnnotationConfigApplicationContext(AppConfig.class); //Get Bean by Type MyController myController = context.getBean(MyController.class); MyRepository myRepository = context.getBean(MyRepository.class); //Call a method on Bean System.out.println(myController.handleRequest()); System.out.println(myRepository.getData()); } }
Output
Service Layer
Data Layer
Summary
In this article, we explore how to configure Spring Beans using Java-based configuration, detailing steps from creating classes to defining them as Beans with @Configuration and @Bean annotations. Using a simple example with a controller, service, and repository, we demonstrate how to set up, manage, and retrieve these Beans within a Spring Boot application.