Step:1. Create New Maven Project
Step:2. Add this context dependency in pom.xml to create console based spring project
org.springframework
spring-context
6.2.7
Step:3. In Spring Project we need to create Spring IOC container in main method.
ApplicationContext context=new AnnotationConfigApplicationContext(“com.shristi”)
Step:4. Add the below codes to display student details in main method
public class StudentMain {
public static void main(String[] args) {
//create the IOCContainer-
//spring beans are created
ApplicationContext context=new AnnotationConfigApplicationContext("com.shristi.constr");
Student student=(Student) context.getBean("student");
System.out.println(student);
}
}
Step:5. Here the student class has dependency with department class. So autowired department class using Constructor DI. For constructor DI no need to provide @Autowired.
Step:6. Student Class has following fields- studentName,studentId, city
Step:7. Department class has following fields- deptId, deptName,deptHead
Step:8. Here inside in the project explorer java/main/resources folder create application.properties. Instaed of hardcording we can give values here.
student.studentName="Ramya"
student.studentId=23456
student.city=Chennai
department.deptId=CSE123
department.deptName="CSE"
department.deptHead="Aditi"
Step:9. To Access the value in the class use @Value(“${student.studentId}”)
Step:10. To add application.properties in class use this line in each class.@PropertySource("classpath:application.properties")
Step:11. Add @Component to create bean in each class if we forgot we get NoSuchBeanDefinitionException
Step:12. Create paramaterized constructor for department
private Department department;
//For Constructor based DI no need to provide @Autowired
public Student(Department department) {
super();
this.department = department;
}
Step:13.create getters and setters method for all fields and toString method too.
Step:14. If it is SetterBased DI we have to create setter method for dependent class reference no getter method and provide @Autowired on top of it.
Student.class
package com.shristi.constr;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
public class Student {
private String studentName;
private int studentId;
private String city;
private Department department;
//For Constructor based DI no need to provide @Autowired
public Student(Department department) {
super();
this.department = department;
}
public String getStudentName() {
return studentName;
}
@Value("${student.studentName}")
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentId() {
return studentId;
}
@Value("${student.studentId}")
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getCity() {
return city;
}
@Value("${student.city}")
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Student [studentName=" + studentName + ", studentId=" + studentId + ", city=" + city + ", department="
+ department + "]";
}
}
Department.java
package com.shristi.constr;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
public class Department {
private String deptId;
private String deptName;
private String deptHead;
public String getDeptId() {
return deptId;
}
@Value("${department.deptId}")
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
@Value("${department.deptName}")
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptHead() {
return deptHead;
}
@Value("${department.deptHead}")
public void setDeptHead(String deptHead) {
this.deptHead = deptHead;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + ", deptHead=" + deptHead + "]";
}
}
Output:
Student [studentName="Ramya", studentId=23456, city=Chennai, department=Department [deptId=CSE123, deptName="CSE", deptHead="Aditi"]]