Cdmlib-remote-webappConfigurationAndBootstrapping » History » Version 27
Andreas Kohlbecker, 02/03/2017 08:41 AM
1 | 1 | Andreas Kohlbecker | # cdmlib-remote-webapp configuration and bootstrapping |
---|---|---|---|
2 | |||
3 | |||
4 | 16 | Andreas Kohlbecker | {{>toc}} |
5 | |||
6 | 2 | Andreas Kohlbecker | This page describes how spring mvc in `cdmlib-remote-webapp` is configured and whow the application is being bootstrapped based on this configuration. |
7 | 1 | Andreas Kohlbecker | |
8 | 15 | Andreas Kohlbecker | ## Bootstrap |
9 | 1 | Andreas Kohlbecker | |
10 | 26 | Andreas Kohlbecker | The bootstraping startst with the [[Cdmlib-remote-webappConfigurationAndBootstrapping#webxml|web.xml]] file in which the _main application context_ and the _DispatcherServlet context_ configured to be picked up by the application container. |
11 | 1 | Andreas Kohlbecker | |
12 | 15 | Andreas Kohlbecker | Spring has support for *hierarchical bean factories*, so in the case of the spring mvc, the dispatcher servlet context is a child of the main application context: |
13 | |||
14 | 26 | Andreas Kohlbecker | * Main application context (beans such as data sources, jpa configuration, business services) --> [[Cdmlib-remote-webappConfigurationAndBootstrapping#applicationContextxml|/WEB-INF/applicationContext.xml]] |
15 | 27 | Andreas Kohlbecker | * -[has child context]-> dispatcher servlet context (MVC specific configuration, components, beans) --> [[Cdmlib-remote-webappConfigurationAndBootstrapping#cdmlib-remote-servletxml|/WEB-INF/cdmlib-remote-servlet.xml]] |
16 | 15 | Andreas Kohlbecker | |
17 | If the servlet context was asked for a bean called "abc" it will look in the servlet context first, if it does not find it there it will look in the parent context, which is the application context. |
||
18 | |||
19 | For very detailed explanation on this topic, see http://stackoverflow.com/questions/15818047/spring-namespace-vs-contextconfiglocation-init-parameters-in-web-xml#answer-15825207 |
||
20 | |||
21 | ### DispatcherServlet context |
||
22 | |||
23 | 1 | Andreas Kohlbecker | The DispatcherServlet builds the MVC context, handles all HTTP requests and delegates them to other components of the MVC architecture. |
24 | 15 | Andreas Kohlbecker | |
25 | 26 | Andreas Kohlbecker | The configuration file for this context is the [[Cdmlib-remote-webappConfigurationAndBootstrapping#cdmlib-remote-servletxml|/WEB-INF/cdmlib-remote-servlet.xml]] |
26 | 15 | Andreas Kohlbecker | |
27 | 22 | Andreas Kohlbecker | ### main application context |
28 | 15 | Andreas Kohlbecker | |
29 | The _main application context_ the the context in which the core cdm application with model, persistence and services is initialized. |
||
30 | 19 | Andreas Kohlbecker | |
31 | 26 | Andreas Kohlbecker | The configuration file for this context is the [[Cdmlib-remote-webappConfigurationAndBootstrapping#applicationContextxml|/WEB-INF/applicationContext.xml]] |
32 | 15 | Andreas Kohlbecker | |
33 | ## Participating classes and configuration files |
||
34 | |||
35 | ### web.xml |
||
36 | 7 | Andreas Kohlbecker | |
37 | The file `/cdmlib-remote-webapp/src/main/webapp/WEB-INF/web.xml` contains an listener entry and the configuration of the Spring dispatcher servlet: |
||
38 | |||
39 | 1 | Andreas Kohlbecker | **ContextLoaderListener:** |
40 | |||
41 | ~~~ |
||
42 | <!-- Creates the Spring Container shared by all Servlets and Filters --> |
||
43 | 15 | Andreas Kohlbecker | <context-param> |
44 | <param-name>contextConfigLocation</param-name> |
||
45 | <param-value>/WEB-INF/applicationContext.xml</param-value> |
||
46 | </context-param> |
||
47 | <listener> |
||
48 | 7 | Andreas Kohlbecker | <!-- loads by default /WEB-INF/applicationContext.xml but this can be overridden above --> |
49 | 15 | Andreas Kohlbecker | <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
50 | 7 | Andreas Kohlbecker | </listener> |
51 | 9 | Andreas Kohlbecker | ~~~ |
52 | 25 | Andreas Kohlbecker | |
53 | 7 | Andreas Kohlbecker | Which creates the Spring Container shared by all Servlets and Filters. The ContextLoaderListener will load the main [[#applicationContextxml|applicationContext configuration file]] `/WEB-INF/applicationContext.xml` |
54 | |||
55 | **DispatcherServlet:** |
||
56 | |||
57 | ~~~ |
||
58 | 1 | Andreas Kohlbecker | <servlet> |
59 | <description>CDM Remote API</description> |
||
60 | <servlet-name>cdmlib-remote</servlet-name> |
||
61 | 7 | Andreas Kohlbecker | <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
62 | <load-on-startup>1</load-on-startup> |
||
63 | 1 | Andreas Kohlbecker | <!-- loads by default policy /WEB-INF/cdmlib-remote-servlet.xml --> |
64 | </servlet> |
||
65 | ~~~ |
||
66 | |||
67 | ### applicationContext.xml |
||
68 | 17 | Andreas Kohlbecker | |
69 | The `cdmlib-remote-webapp` is configured using the [MVC Java Config](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-config-enable) therefore the `applicationContext.xml` does not contain a @ <mvc:annotation-driven/> @ but an `<context:annotation-config />` entry: |
||
70 | |||
71 | * **@<context:annotation-config />@** activates the annotations only on beans which have already been discovered and registered, see http://howtodoinjava.com/2014/07/19/spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/ for detailed explanations. |
||
72 | 1 | Andreas Kohlbecker | |
73 | 11 | Andreas Kohlbecker | `/WEB-INF/applicationContext.xml` |
74 | |||
75 | 8 | Andreas Kohlbecker | ~~~ |
76 | <context:annotation-config /> |
||
77 | ~~~ |
||
78 | 15 | Andreas Kohlbecker | |
79 | ### cdmlib-remote-servlet.xml |
||
80 | 20 | Andreas Kohlbecker | |
81 | This is the configuration of the MVC context of the cdmlib-remote servlet. It contains two parts, of which the first one lets spring find the [[#CdmSpringMVCConfig|CdmSpringMVCConfig]] class which is the main configuration bean for the MVC context. This class defines a couple of component scans to expose further beans to the context: |
||
82 | |||
83 | ~~~ |
||
84 | <!-- |
||
85 | Initialize SpringMVCConfig and its dependency SpringSwaggerConfig. |
||
86 | All further component scans are defined in this mvc configuration class |
||
87 | --> |
||
88 | <context:component-scan base-package="eu/etaxonomy/cdm/remote/config" /> |
||
89 | ~~~ |
||
90 | 21 | Andreas Kohlbecker | In addition to the CdmSpringMVCConfig class this component scan also covers the |
91 | 20 | Andreas Kohlbecker | |
92 | 21 | Andreas Kohlbecker | |
93 | 20 | Andreas Kohlbecker | The second part in this file is a conditional entry is an import directive which is only evaluated when the JVM option `-Dspring.profiles.active=remoting` is supplied. This import causes the initialization of the HTTP Invoker based remoting services needed by the Taxonomic Editor to be able to connect to the CDM Server: |
94 | |||
95 | ~~~ |
||
96 | 1 | Andreas Kohlbecker | <!-- FIXME:Remoting Expose remoting services currently only for testing --> |
97 | <!-- Using Spring Beans Profile (>3.1) http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/ |
||
98 | only valid if it is at the end of the xml file activated by the vmarg -Dspring.profiles.active=remoting --> |
||
99 | <beans profile="remoting"> |
||
100 | <import resource="classpath:/eu/etaxonomy/cdm/remoting-services.xml" /> |
||
101 | 20 | Andreas Kohlbecker | </beans> |
102 | ~~~ |
||
103 | |||
104 | ### CdmSpringMVCConfig |
||
105 | |||
106 | 23 | Andreas Kohlbecker | |
107 | The Java configuration bean for the spring MVC context, that is it is annotated with `@Configuration` but it has not the `@EnableWebMvc` annotation, since it directly subclasses @WebMvcConfigurationSupport@. _"... An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add @Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the Javadoc of @EnableWebMvc. ..."_ |
||
108 | |||
109 | |||
110 | The reason for this is that the method `WebMvcConfigurationSupport.requestMappingHandlerMapping()` needs to be overridden in the @CdmSpringMVCConfig@. We may be able go without this method override once we no longer need CdmAntPathMatcher. which is only needed since the controllers need absolute method level RequestMapping values in some few cases. **TODO** |
||
111 | 21 | Andreas Kohlbecker | |
112 | It enables swagger by the type annotation: |
||
113 | |||
114 | ~~~ |
||
115 | @EnableSwagger |
||
116 | ~~~ |
||
117 | and defines a couple of component scans: |
||
118 | |||
119 | ~~~ |
||
120 | @ComponentScan(basePackages = { |
||
121 | "eu.etaxonomy.cdm.remote.l10n", |
||
122 | "eu.etaxonomy.cdm.remote.controller", |
||
123 | "eu.etaxonomy.cdm.remote.service", |
||
124 | "eu.etaxonomy.cdm.remote.config" |
||
125 | } |
||
126 | ) |
||
127 | ~~~ |
||
128 | |||
129 | |||
130 | ### CdmSwaggerConfig |
||
131 | |||
132 | |||
133 | This is also a spring java configuration class and thus annotated with @@Configuration@. |
||
134 | It provides the cdmlib-remote-webapp specific configuration for swagger. |