Project

General

Profile

Cdmlib-remote-webappConfigurationAndBootstrapping » History » Version 16

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