Skip to content

Commit 1b8a00e

Browse files
alessiostallaKevinGilmore
authored andcommitted
BAEL-82 (eugenp#4198)
* BAEL-82 revised example code * Fix the build * Modified example * Clarified commented out sections of the code.
1 parent afd7079 commit 1b8a00e

11 files changed

Lines changed: 84 additions & 80 deletions
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.contexts.config;
22

3-
import org.springframework.web.context.AbstractContextLoaderInitializer;
43
import org.springframework.web.context.WebApplicationContext;
54
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
65
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
@@ -9,20 +8,29 @@ public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDi
98

109
@Override
1110
protected WebApplicationContext createRootApplicationContext() {
12-
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
13-
rootContext.register(RootApplicationConfig.class);
14-
return rootContext;
11+
//If this is not the only class declaring a root context, we return null because it would clash
12+
//with other classes, as there can only be a single root context.
13+
14+
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
15+
//rootContext.register(RootApplicationConfig.class);
16+
//return rootContext;
17+
return null;
1518
}
1619

1720
@Override
1821
protected WebApplicationContext createServletApplicationContext() {
19-
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
20-
secureWebAppContext.register(SecureWebAppConfig.class);
21-
return secureWebAppContext;
22+
AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
23+
normalWebAppContext.register(NormalWebAppConfig.class);
24+
return normalWebAppContext;
2225
}
2326

2427
@Override
2528
protected String[] getServletMappings() {
26-
return new String[] { "/s/api/*" };
29+
return new String[] { "/api/*" };
30+
}
31+
32+
@Override
33+
protected String getServletName() {
34+
return "normal-dispatcher";
2735
}
2836
}

‎spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@ public class ApplicationInitializer implements WebApplicationInitializer {
1616

1717
@Override
1818
public void onStartup(ServletContext servletContext) throws ServletException {
19-
//XML Context
19+
//Here, we can define a root context and register servlets, among other things.
20+
//However, since we've later defined other classes to do the same and they would clash,
21+
//we leave this commented out.
22+
23+
//Root XML Context
2024
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
2125
//rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml");
2226
//Annotations Context
2327
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
2428
//rootContext.register(RootApplicationConfig.class);
2529
//Registration
2630
//servletContext.addListener(new ContextLoaderListener(rootContext));
27-
28-
XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
29-
normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
30-
ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
31-
normal.setLoadOnStartup(1);
32-
normal.addMapping("/api/*");
31+
32+
//Dispatcher Servlet
33+
//XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
34+
//normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
35+
//ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
36+
//normal.setLoadOnStartup(1);
37+
//normal.addMapping("/api/*");
3338
}
3439

3540
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.web.context.WebApplicationContext;
4+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
5+
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
6+
7+
public class SecureAnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
8+
9+
@Override
10+
protected WebApplicationContext createRootApplicationContext() {
11+
return null;
12+
}
13+
14+
@Override
15+
protected WebApplicationContext createServletApplicationContext() {
16+
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
17+
secureWebAppContext.register(SecureWebAppConfig.class);
18+
return secureWebAppContext;
19+
}
20+
21+
@Override
22+
protected String[] getServletMappings() {
23+
return new String[] { "/s/api/*" };
24+
}
25+
26+
27+
@Override
28+
protected String getServletName() {
29+
return "secure-dispatcher";
30+
}
31+
32+
}

‎spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private void processContext() {
3535
@RequestMapping(path = "/welcome")
3636
public ModelAndView helloWorld() {
3737
processContext();
38-
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
38+
String message = "<br><div style='text-align:center;'>" + "<h3>Normal " + greeterService.greet() + "</h3></div>";
3939
return new ModelAndView("welcome", "message", message);
4040
}
4141
}

‎spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void processContext() {
4343
@RequestMapping(path = "/welcome")
4444
public ModelAndView helloWorld() {
4545
processContext();
46-
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
46+
String message = "<br><div style='text-align:center;'>" + "<h3>Secure " + greeterService.greet() + "</h3></div>";
4747
return new ModelAndView("welcome", "message", message);
4848
}
4949
}

‎spring-all/src/main/java/org/baeldung/controller/config/StudentControllerConfig.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public void onStartup(ServletContext sc) throws ServletException {
1919

2020
root.setServletContext(sc);
2121

22-
// Manages the lifecycle of the root application context
23-
sc.addListener(new ContextLoaderListener(root));
22+
//Manages the lifecycle of the root application context.
23+
//Conflicts with other root contexts in the application, so we've manually set the parent below.
24+
//sc.addListener(new ContextLoaderListener(root));
2425

25-
DispatcherServlet dv = new DispatcherServlet(new GenericWebApplicationContext());
26+
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
27+
webApplicationContext.setParent(root);
28+
DispatcherServlet dv = new DispatcherServlet(webApplicationContext);
2629

2730
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
2831
appServlet.setLoadOnStartup(1);

‎spring-all/src/main/java/org/baeldung/spring/config/MainWebAppInitializer.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ public void onStartup(final ServletContext sc) throws ServletException {
2626
root.scan("org.baeldung.spring.config");
2727
// root.getEnvironment().setDefaultProfiles("embedded");
2828

29-
// Manages the lifecycle of the root application context
30-
sc.addListener(new ContextLoaderListener(root));
29+
//Manages the lifecycle of the root application context.
30+
//Conflicts with other root contexts in the application, so we've manually set the parent below.
31+
//sc.addListener(new ContextLoaderListener(root));
3132

3233
// Handles requests into the application
33-
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
34+
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
35+
webApplicationContext.setParent(root);
36+
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(webApplicationContext));
3437
appServlet.setLoadOnStartup(1);
3538
final Set<String> mappingConflicts = appServlet.addMapping("/");
3639
if (!mappingConflicts.isEmpty()) {

‎spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<beans xmlns="http://www.springframework.org/schema/beans"
2-
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="
5-
http://www.springframework.org/schema/beans
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
65
http://www.springframework.org/schema/beans/spring-beans.xsd
7-
http://www.springframework.org/schema/mvc
8-
http://www.springframework.org/schema/mvc/spring-mvc.xsd
9-
http://www.springframework.org/schema/context
6+
http://www.springframework.org/schema/context
107
http://www.springframework.org/schema/context/spring-context.xsd">
118

129
<context:component-scan base-package="com.baeldung.contexts.normal" />

‎spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<beans xmlns="http://www.springframework.org/schema/beans"
2-
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="
5-
http://www.springframework.org/schema/beans
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
65
http://www.springframework.org/schema/beans/spring-beans.xsd
7-
http://www.springframework.org/schema/mvc
8-
http://www.springframework.org/schema/mvc/spring-mvc.xsd
9-
http://www.springframework.org/schema/context
6+
http://www.springframework.org/schema/context
107
http://www.springframework.org/schema/context/spring-context.xsd">
118

129
<context:component-scan base-package="com.baeldung.contexts.secure" />

‎spring-all/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
application initializers. -->
99
<!--<absolute-ordering>
1010
</absolute-ordering>-->
11-
12-
<!-- load root application context -->
11+
12+
<!-- root application context -->
1313
<!--<listener>
1414
<listener-class>
1515
org.springframework.web.context.ContextLoaderListener

0 commit comments

Comments
 (0)