简体   繁体   English

web.xml 中的 contextConfigLocation 初始化参数

[英]contextConfigLocation init parameters in web.xml

I have a problem with understanding what actually is going on in my project with app context.我在使用应用程序上下文理解我的项目中实际发生的事情时遇到了问题。 I have created new part of project which use spring-mvc我创建了使用 spring-mvc 的项目的新部分

在此处输入图片说明

there are RestConfig class有 RestConfig 类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "restService.com.websystique.springmvc.*")
public class RestConfig {
}

RestServlet class RestServlet 类

@WebServlet(name = "PSC Rest Servlet", urlPatterns = "/rest/*", loadOnStartup = 1)

    public class RestServlet extends DispatcherServlet {
        public RestServlet() {
            super(new GenericWebApplicationContext());
        }
    }

as you can see those classes are servlet and its config.如您所见,这些类是 servlet 及其配置。

all work correctly一切正常

below web.xml whole my project在 web.xml 整个我的项目下面

<?xml ...>
    ...another code
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.psc.config.JpaConfig, com.psc.config.ServicesConfig, restService.com.websystique.springmvc.configuration.RestConfig
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


...another code
</web-app>

I would like to know how my RestConfig context recognises servlet RestServlet (because everything works correctly).我想知道我的 RestConfig 上下文如何识别 servlet RestServlet(因为一切正常)。 I've specified RestConfig in context-param, well than its file will be part of root context.我已经在 context-param 中指定了 RestConfig,它的文件将成为根上下文的一部分。

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.psc.config.JpaConfig, com.psc.config.ServicesConfig, restService.com.websystique.springmvc.configuration.RestConfig
        </param-value>
    </context-param>

But where is RestServlet context?但是 RestServlet 上下文在哪里? If RestConfig is RestServlet context, why it is part of root context?如果 RestConfig 是 RestServlet 上下文,为什么它是根上下文的一部分? Can servlet context be a part of root context? servlet 上下文可以成为根上下文的一部分吗? If yes, my controllers, services and etc will created two times?如果是,我的控制器、服务等将创建两次?

If I specify servlet in web.xml, and not include RestConfig in init-param(context-param) for whole app, I will make good thing or it won't make any changes?如果我在 web.xml 中指定 servlet,并且在整个应用程序的 init-param(context-param) 中不包含 RestConfig,我会做得好还是不会做任何更改?

this is part of log in console这是登录控制台的一部分

11:27:01.183 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started
11:27:01.369 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Thu Aug 11 11:27:01 MSK 2016]; root of context hierarchy
11:27:01.478 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Successfully resolved class for [com.psc.config.JpaConfig]
11:27:01.553 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Successfully resolved class for [com.psc.config.ServicesConfig]
11:27:01.579 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Successfully resolved class for [restService.com.websystique.springmvc.configuration.RestConfig]
11:27:02.843 [localhost-startStop-1] INFO  o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
11:27:03.269 [localhost-startStop-1] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'psc'
11:27:17.430 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/branch/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Branch>> restService.com.websystique.springmvc.controller.BranchController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.432 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/branch],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Branch>> restService.com.websystique.springmvc.controller.BranchController.getBranches(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.434 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/region/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Region>> restService.com.websystique.springmvc.controller.RegionController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.435 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/region],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Region>> restService.com.websystique.springmvc.controller.RegionController.getBranches(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.436 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/region/ || /region],methods=[POST]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Region>> restService.com.websystique.springmvc.controller.RegionController.createUser(com.psc.model.Region)
11:27:17.436 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/region/{id}],methods=[DELETE]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Region>> restService.com.websystique.springmvc.controller.RegionController.deleteRegionById(long)
11:27:17.438 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/role/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Role>> restService.com.websystique.springmvc.controller.RoleController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.439 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/role],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Role>> restService.com.websystique.springmvc.controller.RoleController.getBranches(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.439 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/role/ || /role],methods=[POST]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.Role>> restService.com.websystique.springmvc.controller.RoleController.createUser(com.psc.model.Role)
11:27:17.442 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/roleTree/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.RoleTree>> restService.com.websystique.springmvc.controller.RoleTreeController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.443 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/roleTree],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.RoleTree>> restService.com.websystique.springmvc.controller.RoleTreeController.getBranches(java.lang.Long,java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.443 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/roleTree/ || /roleTree],methods=[POST]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.RoleTree>> restService.com.websystique.springmvc.controller.RoleTreeController.createUser(com.psc.model.RoleTree)
11:27:17.444 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/user/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.User>> restService.com.websystique.springmvc.controller.UserController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.445 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/user],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.User>> restService.com.websystique.springmvc.controller.UserController.getBranches(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.446 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/user/ || /user],methods=[POST]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.User>> restService.com.websystique.springmvc.controller.UserController.createUser(com.psc.model.User)
11:27:17.449 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/userRole/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.UserRole>> restService.com.websystique.springmvc.controller.UserRoleController.getBranchesWithId(java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.450 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/userRole],methods=[GET]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.UserRole>> restService.com.websystique.springmvc.controller.UserRoleController.getBranches(java.lang.Long,java.lang.Long,javax.servlet.http.HttpServletRequest)
11:27:17.451 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/userRole/ || /userRole],methods=[POST]}" onto public org.springframework.http.ResponseEntity<restService.com.websystique.springmvc.model.Box<com.psc.model.UserRole>> restService.com.websystique.springmvc.controller.UserRoleController.createUser(com.psc.model.UserRole)
11:27:18.091 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Thu Aug 11 11:27:01 MSK 2016]; root of context hierarchy
11:27:18.249 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Detected @ExceptionHandler methods in tempControllerAdvice
11:27:18.330 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 17137 ms
11:27:18.419 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Installed AtmosphereHandler com.vaadin.server.communication.PushAtmosphereHandler mapped to context-path: /*
11:27:18.420 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Installed the following AtmosphereInterceptor mapped to AtmosphereHandler com.vaadin.server.communication.PushAtmosphereHandler
11:27:18.430 [localhost-startStop-1] INFO  org.atmosphere.util.IOUtils - META-INF/services/org.atmosphere.cpr.AtmosphereFramework not found in class loader
11:27:18.466 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Atmosphere is using org.atmosphere.util.VoidAnnotationProcessor for processing annotation
11:27:18.497 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Installed WebSocketProtocol org.atmosphere.websocket.protocol.SimpleHttpProtocol 
11:27:18.522 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Installing Default AtmosphereInterceptors
11:27:18.524 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.CorsInterceptor : CORS Interceptor Support
11:27:18.524 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.CacheHeadersInterceptor : Default Response's Headers Interceptor
11:27:18.526 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.PaddingAtmosphereInterceptor : Browser Padding Interceptor Support
11:27:18.527 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.AndroidAtmosphereInterceptor : Android Interceptor Support
11:27:18.527 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Dropping Interceptor org.atmosphere.interceptor.HeartbeatInterceptor
11:27:18.528 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.SSEAtmosphereInterceptor : SSE Interceptor Support
11:27:18.529 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.JSONPAtmosphereInterceptor : JSONP Interceptor Support
11:27:18.532 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.JavaScriptProtocol : Atmosphere JavaScript Protocol
11:27:18.533 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.WebSocketMessageSuspendInterceptor : org.atmosphere.interceptor.WebSocketMessageSuspendInterceptor
11:27:18.535 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.OnDisconnectInterceptor : Browser disconnection detection
11:27:18.536 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework -   org.atmosphere.interceptor.IdleResourceInterceptor : org.atmosphere.interceptor.IdleResourceInterceptor
11:27:18.537 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Set org.atmosphere.cpr.AtmosphereInterceptor.disableDefaults to disable them.
11:27:18.545 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Using EndpointMapper class org.atmosphere.util.DefaultEndpointMapper
11:27:18.545 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Using BroadcasterCache: org.atmosphere.cache.UUIDBroadcasterCache
11:27:18.546 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Default Broadcaster Class: org.atmosphere.cpr.DefaultBroadcaster
11:27:18.547 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Broadcaster Polling Wait Time 100
11:27:18.547 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Shared ExecutorService supported: true
11:27:18.548 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Messaging Thread Pool Size: Unlimited
11:27:18.548 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Async I/O Thread Pool Size: 200
11:27:18.548 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Using BroadcasterFactory: org.atmosphere.cpr.DefaultBroadcasterFactory
11:27:18.548 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Using WebSocketProcessor: org.atmosphere.websocket.DefaultWebSocketProcessor
11:27:18.549 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Invoke AtmosphereInterceptor on WebSocket message true
11:27:18.549 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - HttpSession supported: true
11:27:18.549 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Atmosphere is using DefaultAtmosphereObjectFactory for dependency injection and object creation
11:27:18.550 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Atmosphere is using async support: org.atmosphere.container.Tomcat7Servlet30SupportWithWebSocket running under container: Apache Tomcat/7.0.47 using javax.servlet/3.0
11:27:18.550 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Atmosphere Framework 2.2.7.vaadin1 started.
11:27:18.555 [localhost-startStop-1] INFO  o.atmosphere.cpr.AtmosphereFramework - Installed AtmosphereInterceptor  Track Message Size Interceptor using | with priority BEFORE_DEFAULT 
11:27:18.579 [localhost-startStop-1] INFO  o.a.c.c.C.[.[.[/ProductCatalog] - Initializing Spring FrameworkServlet 'PSC Rest Servlet'
11:27:18.580 [localhost-startStop-1] INFO  r.c.w.springmvc.RestServlet - FrameworkServlet 'PSC Rest Servlet': initialization started
11:27:18.584 [localhost-startStop-1] INFO  o.s.w.c.s.GenericWebApplicationContext - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@274638b4: startup date [Thu Aug 11 11:27:18 MSK 2016]; parent: Root WebApplicationContext
11:27:18.602 [localhost-startStop-1] INFO  r.c.w.springmvc.RestServlet - FrameworkServlet 'PSC Rest Servlet': initialization completed in 22 ms
11:27:18.631 [main] INFO  o.a.coyote.http11.Http11Protocol - Starting ProtocolHandler ["http-bio-8080"]

From what I understand of your code, you've specified that all beans should be retrieved through the context loader listener, which acts like a global context, from which all dispatcher servlets can retrieve beans.根据我对您代码的理解,您已指定所有 bean 都应通过上下文加载器侦听器检索,该侦听器的作用类似于全局上下文,所有调度程序 servlet 都可以从中检索 bean。

Usually, a dispatcher servlet is backed by an XML context whose default file name is derived from the name you give the servlet in web.xml.通常,调度程序 servlet 由 XML 上下文支持,该上下文的默认文件名派生自您在 web.xml 中为 servlet 指定的名称。 If the dispatcher servlet is called dispatcher then the file name is dispatcher-servlet.xml .如果调度程序 servlet 称为dispatcher则文件名为dispatcher-servlet.xml Since you're not declaring any specific context for PSC Rest Servlet , you're only benefitting from the top-level context.由于您没有为PSC Rest Servlet声明任何特定的上下文,因此您只会从顶级上下文中受益。

As for the rest of your questions.至于你的其余问题。 There is only one definition of your beans at the moment.目前您的 bean 只有一个定义。 If you decide to split between a top-level context (application context provided by context loader listener) and o lower-level context accessible only to your dispatcher servlet, you should take care not to have 2 definitions of the same bean.如果您决定在顶级上下文(由上下文加载器侦听器提供的应用程序上下文)和只能由您的调度程序 servlet 访问的低级上下文之间进行拆分,您应该注意不要有同一个 bean 的 2 个定义。 One way to structure your contexts is to split them by themes and decide which are solely used by the web aspect of your app, which are for the database, which are cross-cutting concerns, like logging, monitoring or transactions... There is no single way to do it.构建上下文的一种方法是按主题拆分它们,并决定哪些仅由应用程序的 Web 方面使用,哪些用于数据库,哪些是横切关注点,例如日志记录、监控或事务......有没有单一的方法可以做到。 You could have transactions, services and repositories in the application context, while things like marshallers, converters, controllers and the like stay on the dispatcher side.您可以在应用程序上下文中拥有事务、服务和存储库,而诸如编组器、转换器、控制器之类的东西则留在调度程序端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM