"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : JavaBasedConfiguration Date : 21/03/2024 (Session - 09) _____________________________________________________________________________________________________________________________ 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. * @PropertySource is used to load the properties file into Spring container. * @Value is used to extract the value from properties file based on supplied key. SpringConfig.java ================= package com.ashokit.spring.config; import java.util.Date; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; import com.ashokit.spring.beans.Demo; import com.ashokit.spring.beans.User; //Representating the Java class as spring configuration class @Configuration //Recognizing the Reading properties file by the spring container @PropertySource(value = "applicationConfig.properties") //Importing the one configuration class bean definition into another configuration class @Import(value = {DatabaseConfig.class,EmailConfig.class}) //Importing the spring bean definitions from xml configuration file @ImportResource(value = "spring.xml") public class SpringConfig { //Reading property value from properties file @Value("${user.username}") private String username; @Value("${user.password}") private String password; //User class as spring Bean @Bean public User getUserObject() { User user = new User(); user.setUsername(username); user.setPassword(password); return user; } @Bean public Demo getDemoObject() { //return new Demo("Mahesh","Mahesh@123"); return new Demo(); } @Bean public Date getDateObject() { return new java.util.Date(); } } applicationConfig.properties ============================ #Defining the some application specific configuration user.username=Mahesh user.password=Mahesh@12311111 spring.xml =========== SpringClient.java ================= package com.ashokit.spring.client; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.ashokit.spring.beans.Address; import com.ashokit.spring.beans.Course; import com.ashokit.spring.beans.Demo; import com.ashokit.spring.beans.Student; import com.ashokit.spring.beans.User; import com.ashokit.spring.config.DatabaseConfig; import com.ashokit.spring.config.EmailConfig; import com.ashokit.spring.config.SpringConfig; public class SpringClient { public static void main(String[] args) { //Creating container object and passing java based configuration class ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); //Request spring bean object based on object type User user = context.getBean(User.class); System.out.println(user); System.out.println(user.hashCode()); User user1 = context.getBean(User.class); System.out.println(user1.hashCode()); User user2 = context.getBean(User.class); System.out.println(user2.hashCode()); Demo demo = context.getBean(Demo.class); System.out.println(demo); Student st = context.getBean(Student.class); System.out.println(st); Course ct = context.getBean(Course.class); System.out.println(ct); Address address = context.getBean(Address.class); System.out.println(address); } } Output ====== Student Class.... Course Class... User [username=Mahesh, password=Mahesh@12311111] 1740223770 1740223770 1740223770 Demo [username=null, password=null] com.ashokit.spring.beans.Student@1205bd62 com.ashokit.spring.beans.Course@7ef27d7f Address [doorNo=1-2-3, cityName=Chennai, pincode=12324]