What is container in spring framework?

久未见 提交于 2019-12-03 06:32:43

问题


  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 container in this article? Is it a piece of code or the bean config file?

  2. Can Spring IOC be used in Spring MVC?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/8708529/what-is-container-in-spring-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!