Project

General

Profile

Cdmlib-remote-webappConfigurationAndBootstrapping » History » Version 22

Andreas Kohlbecker, 11/26/2015 12:15 PM

1 1 Andreas Kohlbecker
2
# cdmlib-remote-webapp configuration and bootstrapping
3
4
5 16 Andreas Kohlbecker
{{>toc}}
6
7
8
9 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.  
10 1 Andreas Kohlbecker
11
12 2 Andreas Kohlbecker
13 15 Andreas Kohlbecker
## Bootstrap
14 1 Andreas Kohlbecker
15
16 18 Andreas Kohlbecker
The bootstraping startst with the [[#webxml|web.xml]] file in which the _main application context_ and the _DispatcherServlet context_ configured to be picked up by the application container.
17 1 Andreas Kohlbecker
18 15 Andreas Kohlbecker
19
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:
20
21 19 Andreas Kohlbecker
* Main application context (beans such as data sources, jpa configuration, business services) --> [[#applicationContextxml|/WEB-INF/applicationContext.xml]]
22 15 Andreas Kohlbecker
23 19 Andreas Kohlbecker
  * -[[has|child context]]-> dispatcher servlet context (MVC specific configuration, components, beans) --> [[#cdmlib-remote-servletxml|/WEB-INF/cdmlib-remote-servlet.xml]]
24 15 Andreas Kohlbecker
25
26
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.
27
28
29
For very detailed explanation on this topic, see http://stackoverflow.com/questions/15818047/spring-namespace-vs-contextconfiglocation-init-parameters-in-web-xml#answer-15825207
30
31
32
33
### DispatcherServlet context
34
35
36 1 Andreas Kohlbecker
The DispatcherServlet builds the MVC context, handles all HTTP requests and delegates them to other components of the MVC architecture.
37 15 Andreas Kohlbecker
38 19 Andreas Kohlbecker
39 15 Andreas Kohlbecker
The configuration file for this context is the [[#cdmlib-remote-servletxml|/WEB-INF/cdmlib-remote-servlet.xml]]
40
41
42
43 22 Andreas Kohlbecker
### main application context
44 15 Andreas Kohlbecker
45 1 Andreas Kohlbecker
46 15 Andreas Kohlbecker
The _main application context_ the the context in which the core cdm application with model, persistence and services is initialized.  
47 19 Andreas Kohlbecker
48 15 Andreas Kohlbecker
49
The configuration file for this context is the [[#applicationContextxml|/WEB-INF/applicationContext.xml]]
50
51
 
52
53
54
## Participating classes and configuration files
55
56
57
58 1 Andreas Kohlbecker
### CdmSpringMVCConfig
59 7 Andreas Kohlbecker
60
61 6 Andreas Kohlbecker
The main configuration class `CdmSpringMVCConfig` 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. ..."_
62
63
64
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 contollers need absolute method level RequestMapping values in some few cases. **TODO** 
65
66 11 Andreas Kohlbecker
67 7 Andreas Kohlbecker
68
69
### web.xml
70
71
The file `/cdmlib-remote-webapp/src/main/webapp/WEB-INF/web.xml` contains an listener entry and the configuration of the Spring dispatcher servlet:
72
73 1 Andreas Kohlbecker
74
 **ContextLoaderListener:** 
75
76
~~~
77 15 Andreas Kohlbecker
<!-- Creates the Spring Container shared by all Servlets and Filters -->
78
<context-param>
79
  <param-name>contextConfigLocation</param-name>
80
  <param-value>/WEB-INF/applicationContext.xml</param-value>
81
</context-param>
82 7 Andreas Kohlbecker
<listener>
83 15 Andreas Kohlbecker
    <!-- loads by default /WEB-INF/applicationContext.xml but this can be overridden above -->
84 7 Andreas Kohlbecker
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
85 9 Andreas Kohlbecker
</listener>
86 7 Andreas Kohlbecker
~~~
87
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` 
88
89
90
 **DispatcherServlet:** 
91
92
~~~
93 1 Andreas Kohlbecker
<servlet>
94
    <description>CDM Remote API</description>
95
    <servlet-name>cdmlib-remote</servlet-name>
96 7 Andreas Kohlbecker
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
97
    <load-on-startup>1</load-on-startup>
98 1 Andreas Kohlbecker
    <!-- loads by default policy /WEB-INF/cdmlib-remote-servlet.xml  -->
99
</servlet>
100
~~~
101
102
103
### applicationContext.xml
104 17 Andreas Kohlbecker
105
106
107
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:
108
109
110
* **@<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.
111 8 Andreas Kohlbecker
112 1 Andreas Kohlbecker
113 11 Andreas Kohlbecker
 `/WEB-INF/applicationContext.xml` 
114
115
116 8 Andreas Kohlbecker
~~~
117
<context:annotation-config />
118
~~~
119 15 Andreas Kohlbecker
120
121
### cdmlib-remote-servlet.xml
122 20 Andreas Kohlbecker
123
124
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:
125
126
~~~
127
<!-- 
128
  Initialize SpringMVCConfig and its dependency SpringSwaggerConfig.
129
  All further component scans are defined in this mvc configuration class 
130
-->
131
<context:component-scan base-package="eu/etaxonomy/cdm/remote/config" />
132
~~~
133 21 Andreas Kohlbecker
In addition to the CdmSpringMVCConfig class this component scan also covers the 
134 20 Andreas Kohlbecker
135 21 Andreas Kohlbecker
136 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:
137
138
139
~~~
140
<!-- FIXME:Remoting Expose remoting services currently only for testing -->
141
<!-- Using Spring Beans Profile (>3.1) http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/ 
142
  only valid if it is at the end of the xml file activated by the vmarg -Dspring.profiles.active=remoting -->
143
<beans profile="remoting">
144
  <import resource="classpath:/eu/etaxonomy/cdm/remoting-services.xml" />
145
</beans>
146
~~~
147 21 Andreas Kohlbecker
148
149
### CdmSpringMVCConfig
150
 
151
152
This class is a Java configuration bean for spring, that is it is annotated with `@Configuration` and subclasses `WebMvcConfigurationSupport` by which it becomes main class providing the configuration behind the MVC Java config.
153
154
155
It enables swagger by the type annotation:
156
157
~~~
158
@EnableSwagger
159
~~~
160
and defines a couple of component scans:
161
162
~~~
163
@ComponentScan(basePackages = {
164
        "eu.etaxonomy.cdm.remote.l10n",
165
        "eu.etaxonomy.cdm.remote.controller",
166
        "eu.etaxonomy.cdm.remote.service",
167
        "eu.etaxonomy.cdm.remote.config"
168
        }
169
)
170
~~~
171
172
173
### CdmSwaggerConfig
174
 
175
176
This is also a spring java configuration class and thus annotated with @@Configuration@.
177
178
It provides the cdmlib-remote-webapp specific configuration for swagger.