Project

General

Profile

Cdmlib-remote-webappConfigurationAndBootstrapping » History » Version 18

Andreas Kohlbecker, 11/26/2015 11:56 AM

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
* Main application context (beans such as data sources, jpa configuration, business services)
22
23
  * -[[has|child context]]-> dispatcher servlet context (MVC specific configuration, components, beans)
24
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
The DispatcherServlet builds the MVC context, handles all HTTP requests and delegates them to other components of the MVC architecture.
37
38
The configuration file for this context is the [[#cdmlib-remote-servletxml|/WEB-INF/cdmlib-remote-servlet.xml]]
39
40
41
42
## main application context
43
44
45
The _main application context_ the the context in which the core cdm application with model, persistence and services is initialized.  
46
47
The configuration file for this context is the [[#applicationContextxml|/WEB-INF/applicationContext.xml]]
48
49
 
50
51
52
## Participating classes and configuration files
53
54
55
56 1 Andreas Kohlbecker
### CdmSpringMVCConfig
57 7 Andreas Kohlbecker
58
59 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. ..."_
60
61
62
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** 
63
64 11 Andreas Kohlbecker
65 7 Andreas Kohlbecker
66
67
### web.xml
68
69
The file `/cdmlib-remote-webapp/src/main/webapp/WEB-INF/web.xml` contains an listener entry and the configuration of the Spring dispatcher servlet:
70
71 1 Andreas Kohlbecker
72
 **ContextLoaderListener:** 
73
74
~~~
75 15 Andreas Kohlbecker
<!-- Creates the Spring Container shared by all Servlets and Filters -->
76
<context-param>
77
  <param-name>contextConfigLocation</param-name>
78
  <param-value>/WEB-INF/applicationContext.xml</param-value>
79
</context-param>
80 7 Andreas Kohlbecker
<listener>
81 15 Andreas Kohlbecker
    <!-- loads by default /WEB-INF/applicationContext.xml but this can be overridden above -->
82 7 Andreas Kohlbecker
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
83 9 Andreas Kohlbecker
</listener>
84 7 Andreas Kohlbecker
~~~
85
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` 
86
87
88
 **DispatcherServlet:** 
89
90
~~~
91 1 Andreas Kohlbecker
<servlet>
92
    <description>CDM Remote API</description>
93
    <servlet-name>cdmlib-remote</servlet-name>
94 7 Andreas Kohlbecker
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
95
    <load-on-startup>1</load-on-startup>
96 1 Andreas Kohlbecker
    <!-- loads by default policy /WEB-INF/cdmlib-remote-servlet.xml  -->
97
</servlet>
98
~~~
99
100
101
### applicationContext.xml
102 17 Andreas Kohlbecker
103
104
105
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:
106
107
108
* **@<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.
109 8 Andreas Kohlbecker
110 1 Andreas Kohlbecker
111 11 Andreas Kohlbecker
 `/WEB-INF/applicationContext.xml` 
112
113
114 8 Andreas Kohlbecker
~~~
115
<context:annotation-config />
116
~~~
117 15 Andreas Kohlbecker
118
119
### cdmlib-remote-servlet.xml