"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : JavaBasedConfiguration Date : 31/08/2024 (Session - 14) _____________________________________________________________________________________________________________________________ Last Session ============= * We discussed about ApplicationContext Container Today Session ============== Java Based Configuration Development ===================================== * In addition to XML-based configuration, Spring also supports Java-based configuration and its new style of developing Spring Application. * It allows you to configure your Spring beans and dependencies using Java code. * Java-based configuration provides a more programmatic and type-safe approach compared to XML configuration. Steps for Developing Java Based Configuration Application ========================================================= 1) Enable Java-based configuration: =================================== * To enable Java-based configuration in your Spring application, you need to add the @Configuration annotation to a class. * This class will serve as your configuration class similarly to xml configuration file. Example ======= @Configuration public class AppConfig { // Bean definitions and configuration methods } 2) Define beans: ================ * In your configuration class, you can define beans using the @Bean annotation on methods. The return type of the method represents the type of the bean, and the method body contains the logic to create and configure the bean. Example ======= @Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } @Bean public AnotherBean anotherBean() { return new AnotherBean(); } } 3) Main Program =============== * We can create the Main Program by Creating object of ApplicationContext with its implementation Class AnnotationConfigApplicationContext. * AnnotationConfigApplicationContext implementation class having capability to read the configurations from Java based configuration class. * We need to provide the Configuration class to AnnotationConfigApplicationContext class. Example ====== public class MainClient{ public static void main(String[] args){ ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); //Requesting the Spring bean logic } } Example Application on Java Based Configuration =============================================== User.java ========= package com.ashokit.beans; public class User { private String username; private String password; //setter injection public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return String.format("Username %s and password %s",username,password); } } Student.java ============ package com.ashokit.beans; public class Student { //simple properties private String id; private String name; private String emailId; //Object private Course course; //setter injection public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setEmailId(String emailId) { this.emailId = emailId; } public void setCourse(Course course) { this.course = course; } @Override public String toString() { return String.format("StudentId %s and StudentName %s and EmailId %s and CourseName %s",id,name,emailId,course.getCourseName()); } } Course.java =========== package com.ashokit.beans; public class Course { private String courseName; private Integer courseFee; public void setCourseName(String courseName) { this.courseName = courseName; } public void setCourseFee(Integer courseFee) { this.courseFee = courseFee; } public String getCourseName() { return courseName; } public Integer getCourseFee() { return courseFee; } @Override public String toString() { return String.format("CourseName %s and CourseFee %s ", courseName,courseFee); } } SpringConfig.java ================= package com.ashokit.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.ashokit.beans.Course; import com.ashokit.beans.Student; import com.ashokit.beans.User; //This will acts as Spring Configuration File @Configuration public class SpringConfig { //creating the Java method to represent as Spring bean i.e.,User @Bean public User getUserObject() { //return new User(); User user = new User(); user.setUsername("Mahesh"); user.setPassword("Mahesh@123"); return user; } //creating the Java method to represent as Spring bean i.e.,Address @Bean public Student getStudentObject() { Student st = new Student(); st.setId("AIT123"); st.setName("Mahesh"); st.setEmailId("mahesh.ashokit@gmail.com"); //Injecting the courseBean into student Bean st.setCourse(getCourseBean()); return st; } @Bean public Course getCourseBean() { Course course = new Course(); course.setCourseName("SpringBootMicroservices"); course.setCourseFee(8000); return course; } } SpringClient.java ================= import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.ashokit.beans.Student; import com.ashokit.beans.User; import com.ashokit.config.SpringConfig; public class SpringClient { public static void main(String[] args) { //creating the object for spring container and passing Java Based Configuration class ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); //Requesting the Spring beans User user = context.getBean(User.class); System.out.println(user.hashCode()); User user1 = context.getBean(User.class); System.out.println(user1.hashCode()); Student student = context.getBean(Student.class); System.out.println(student.hashCode()); Student student1 = context.getBean(Student.class); System.out.println(student1.hashCode()); } } OUTPUT ====== Username Mahesh and password Mahesh@123 StudentId AIT123 and StudentName Mahesh and EmailId mahesh.ashokit@gmail.com and CourseName SpringBootMicroservices Inheriting configuration classes ================================ package com.ashokit.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import com.ashokit.beans.Course; import com.ashokit.beans.Student; import com.ashokit.beans.User; //This will acts as Spring Configuration File @Configuration @Import(value = {DatabaseConfig.class,EmailConfig.class}) //Inheriting the configuration from DatabaseConfig class public class SpringConfig { //creating the Java method to represent as Spring bean i.e.,User @Bean public User getUserObject() { //return new User(); User user = new User(); user.setUsername("Mahesh"); user.setPassword("Mahesh@123"); return user; } //creating the Java method to represent as Spring bean i.e.,Address @Bean public Student getStudentObject() { Student st = new Student(); st.setId("AIT123"); st.setName("Mahesh"); st.setEmailId("mahesh.ashokit@gmail.com"); //Injecting the courseBean into student Bean st.setCourse(getCourseBean()); return st; } @Bean public Course getCourseBean() { Course course = new Course(); course.setCourseName("SpringBootMicroservices"); course.setCourseFee(8000); return course; } } NOTE ==== * @Import Annotation is used to import the spring bean definitions from another Spring Configuration classes. * @ImportResource annotation is used to import the Spring bean definitions from XML file. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++