What is container in spring framework?

前端 未结 5 1591
南旧
南旧 2021-02-01 04:30
  1. I was going through this article http://www.vaannila.com/spring/spring-ioc-1.html and here the term container is used. The diagram below shows container. What is contai

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 05:10

    Let me explain what the spring container is ..suppose you have a java application with one class named Student and with one variable student name. here we go

    public class Student{
     private String name;
     public void setName(String name){
          this.name  = name;
     public void getName(){
          System.out.println("Your Name : " + name);}}
    

    Now you want the name variable should get automatically initialized to iqbal when application runs and the student object should be available inside the main class.

    • write an xml configuration file where you will define this Student object.

    
    
    
    
       
          
       
    
    

    Now inside the main class we have ApplicationContext

    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          Studentobj = (Student) context.getBean("student");
          obj.getMessage();
       }
    }

    SO please note here ApplicationContext ,this will act as a container and will create and manage the Student class for your application.

提交回复
热议问题