Spring + Web MVC: dispatcher-servlet.xml vs. applicationContext.xml (plus shared security)

后端 未结 6 2073
有刺的猬
有刺的猬 2020-11-29 15:17

What is the correct way to use the two contexts: dispatcher-servlet.xml and applicationContext.xml? What goes where?

I want to write a fair

6条回答
  •  攒了一身酷
    2020-11-29 15:43

    The dispatcher-servlet.xml file contains all of your configuration for Spring MVC. So in it you will find beans such as ViewHandlerResolvers, ConverterFactories, Interceptors and so forth. All of these beans are part of Spring MVC which is a framework that structures how you handle web requests, providing useful features such as databinding, view resolution and request mapping.

    The application-context.xml can optionally be included when using Spring MVC or any other framework for that matter. This gives you a container that may be used to configure other types of spring beans that provide support for things like data persistence. Basically, in this configuration file is where you pull in all of the other goodies Spring offers.

    These configuration files are configured in the web.xml file as shown:

    Dispatcher Config

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

    Application Config

    
        contextConfigLocation
        /WEB-INF/spring/application-context.xml
    
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    

    To configure controllers, annotate them with @Controller then include the following in the dispatcher-context.xml file:

    
    
    

提交回复
热议问题