What is container in spring framework?

情到浓时终转凉″ 提交于 2019-12-02 18:10:38
Ralph

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.

Aravind A

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.

kosa

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

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.

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