"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Autowiring Date : 18/03/2024 (Session - 06) _____________________________________________________________________________________________________________________________ Today Session : Bean Autowiring =============================== * Bean wiring is nothing but configuring the dependencies of abean along with bean in xml file. * If you manually configure bean dependencies into an xml file then the size of xml file will be increased. * Inorder to cut down the xml configuration the framework has introduced the autowiring facility. * Incase of autowiring we no need to configure the dependencies explictly in xml file based on autowire strategy the container will inject the dependencies of abean automatically. * Bean autowiring facility is only applicable for "Dependency in the form of Objects". * To add autowiring facility we need to configure "autowire" attribute with tag. * The values of autowire attribute are 1) no 2) byName 3) byType 4) constructor * Bean Autowiring facility can be achieved using two approaches 1) XML Approach 2) Annotation Approach Student.java ============ package com.ashokit.spring.beans; public class Student { //simple properties private String studentId; private String name; //object properties private Address address; private Course course; public void setStudentId(String studentId) { this.studentId = studentId; } public void setName(String name) { this.name = name; } public void setAddress(Address address) { this.address = address; } public void setCourse(Course course) { this.course = course; } @Override public String toString() { return "Student [studentId=" + studentId + ", name=" + name + ", address=" + address + ", course=" + course + "]"; } } Address.java ============ package com.ashokit.spring.beans; public class Address { private String doorNo; private String cityName; private String pincode; public void setDoorNo(String doorNo) { this.doorNo = doorNo; } public void setCityName(String cityName) { this.cityName = cityName; } public void setPincode(String pincode) { this.pincode = pincode; } @Override public String toString() { return "Address [doorNo=" + doorNo + ", cityName=" + cityName + ", pincode=" + pincode + "]"; } } Course.java ========== package com.ashokit.spring.beans; public class Course { private String courseId; private String courseName; private int courseFee; public void setCourseId(String courseId) { this.courseId = courseId; } public void setCourseName(String courseName) { this.courseName = courseName; } public void setCourseFee(int courseFee) { this.courseFee = courseFee; } @Override public String toString() { return "Course [courseId=" + courseId + ", courseName=" + courseName + ", courseFee=" + courseFee + "]"; } } spring.xml ========== SpringClient.java ================= package com.ashokit.spring.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ashokit.spring.beans.Student; public class SpringClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Student student = (Student)context.getBean("stu"); System.out.println(student); } }