ApplicationContext and ServletContext

前端 未结 5 578
孤独总比滥情好
孤独总比滥情好 2020-12-23 09:55

I get confused between the two ApplicationContext and ServletContext when it comes to Spring MVC Application. I know that There is just only One ApplicationContext per Sprin

5条回答
  •  情歌与酒
    2020-12-23 10:03

    Servlet Context:

    It is initialized when an Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params etc) of the whole servlet application.

    Application Context:

    It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the of the beans that is defined inside the spring configuration files. Servlet-Context has no idea about this things.

    There are two types of contexts in Spring parent and child.

    Spring Parent Context (Application Context / Root Context )

      
             
                org.springframework.web.context.ContextLoaderListener
            
      
      
            contextConfigLocation
            
                /WEB-INF/service-context.xml,
                /WEB-INF/dao-context.xml,
                /WEB-INF/was-context.xml,
                /WEB-INF/jndi-context.xml,
                /WEB-INF/json-context.xml
            
      
    

    role-purpose-of-contextloaderlistener-in-spring
    Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
    When spring container start ups, it reads all the bean definitions from the configuration files and creates beans objects and manages life cycle of the beans objects. This configuration is totally optional.

    DispatcherServlet vs ContextLoaderListener
    /declaring-spring-bean-in-parent-context-vs-child-context

    Spring Child Context ( WebApplicationContext / Child Context )

    
        myWebApplication
        
             org.springframework.web.servlet.DispatcherServlet
        
        1
    
    
        myWebApplication
        /app/*
    
    

    When spring web application starts it will look for spring bean configuration file myWebApplication-servlet.xml. It will read all the bean definitions and create and manages the bean objects life cycle. If parent spring context is available it will merge the child spring context with the parent spring context. If there is no Spring parent context available the application will only have the child spring context.

提交回复
热议问题