About multiple containers in spring framework

前端 未结 4 2059
情歌与酒
情歌与酒 2020-12-04 07:15

In a typical Spring MVC project there two \"containers\": One created by ContextLoaderListener and the other created by DispatchServlet.

I want to know, are these re

4条回答
  •  悲哀的现实
    2020-12-04 07:36

    There aren't two separate containers created. Typically, you want spring to instantiate the object declared in the servlet-context.xml when the object is required. So, you map the servlet-context.xml configuration file to the Dispatcher Servlet i.e. you want to initialize the object when a request hits the dispatcher servlet.

    
        appServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/spring/appServlet/servlet-context.xml
        
        1
    
    

    Where as, if you want to initialize the object and perform action when the context is being loaded you would declare the configuration file with in the context-param tags of your deployment descriptor.

    
        contextConfigLocation
        /WEB-INF/spring/root-context.xml
    
    

    You could test this out by writing by declaring separate beans in the servlet-context.xml and root-context.xml and then, autowiring them in a custom Context Loader Listener class. You would find only the root-context instances are initialized and servlet-context beans are null.

提交回复
热议问题