public class Student { private int id; private String name; public Student() {} public Student(int id) { this.id = id; } public Student(String name) { this.name = name; } public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void display() { System.out.println("Name is: " + name + ", and ID is: " + id); } }
Without Spring, we create a Student instance like -
Student student = new Student(); student.setId(0); student.setName("Tom")
With Spring, Spring container creates instance, and we use getBean to get the instance.
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Student student = (Student) context.getBean("student");
spring.xml -
More explanation about Application Context container can be found at spring_applicationcontext_container.
No comments:
Post a Comment