1
2
3
4
5 package org.srinivas.siteworks.rlrestserver.webconfig;
6
7 import javax.servlet.ServletContext;
8 import javax.servlet.ServletRegistration;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.web.WebApplicationInitializer;
13 import org.springframework.web.context.ContextLoaderListener;
14 import org.springframework.web.context.WebApplicationContext;
15 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
16 import org.springframework.web.servlet.DispatcherServlet;
17
18
19
20
21 public class RLRestServerWebInitializer implements WebApplicationInitializer {
22
23 private static final Logger logger = LoggerFactory.getLogger(RLRestServerWebInitializer.class);
24
25
26
27
28 @Override
29 public void onStartup(ServletContext container) {
30 logger.info("Started to pickup the annotated classes at RLRestServerWebInitializer");
31 startServlet(container);
32 }
33
34
35
36
37
38
39 private void startServlet(final ServletContext container) {
40 WebApplicationContext dispatcherContext = registerContext(RLRestServerMvcContextConfiguration.class);
41 DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
42 container.addListener(new ContextLoaderListener(dispatcherContext));
43 container.addListener(new RLRestServerCamelListener());
44 ServletRegistration.Dynamic dispatcher;
45 dispatcher = container.addServlet("dispatcher", dispatcherServlet);
46 dispatcher.setLoadOnStartup(1);
47 dispatcher.addMapping("/*");
48 }
49
50
51
52
53
54
55
56 private WebApplicationContext registerContext(final Class<?>... annotatedClasses) {
57 logger.info("Using AnnotationConfigWebApplicationContext createContext");
58 AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
59 context.register(annotatedClasses);
60 return context;
61 }
62
63 }