What is container in spring framework?

前端 未结 5 1584
南旧
南旧 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 04:54

    I like this definition:

    Spring IoC Container The core of the Spring Application Framework is its Inversion of Control (IoC) Container. Its job is to instantiate, initialize, and wire up objects of the application, as well as provide lots of other features available in Spring throughout an object's lifetime. The objects that form the backbone of your application, and are managed by Spring Container, are called beans. They are ordinary Java objects— also known as POJOs— but they are instantiated, assembled by the Spring Container, and managed within it.

    Caliskan, Mert, and Kenan Sevindik. Beginning Spring, John Wiley & Sons, Incorporated, 2015.

    Short Spring IoC Container is box where Beans live.

    0 讨论(0)
  • 2021-02-01 04:57

    Container is piece of code which reads bean config file and performs corresponding actions.

    Yes IOC can be used with MVC. Here is an article about it. spring mvc

    0 讨论(0)
  • 2021-02-01 05:05

    In this context, a container has the meaning of something that provides an infrastructrure needed by some components to live.

    You can imagine it this way:

    • Like the JVM is a container to run Java Programs,
    • A servlet container (i.e. Tomcat) is the thing that runs servlets
    • An EJB-Container is the environmet where EJB live (see this wikipedia article (in german, but you can use your browser translator))

    The same way Spring is the container where Spring Beans live.

    0 讨论(0)
  • 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.

    <?xml version = "1.0" encoding = "UTF-8"?>
    
    <beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id = "Student" class = "com.packagename.Student">
          <property name = "name" value = "iqbal"/>
       </bean>
    
    </beans>

    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.

    0 讨论(0)
  • 2021-02-01 05:20

    Container is used to describe any component that can contain other components inside itself.

    As per the Spring documentation here

    The BeanFactory interface is the central IoC container interface in Spring. Its
    responsibilities include instantiating or sourcing application objects, configuring such objects, and assembling the dependencies between these objects.

    IOC is the core principle which Spring uses for Separation of concern concept . No matter what you use - Spring MVC, Security , Core , DAO integrations , you will be using the IOC principle.

    0 讨论(0)
提交回复
热议问题