BAEL-82 (#4198)
* BAEL-82 revised example code * Fix the build * Modified example * Clarified commented out sections of the code.
This commit is contained in:
parent
afd7079968
commit
1b8a00e6d4
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.web.context.AbstractContextLoaderInitializer;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
||||
|
@ -9,20 +8,29 @@ public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDi
|
|||
|
||||
@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
rootContext.register(RootApplicationConfig.class);
|
||||
return rootContext;
|
||||
//If this is not the only class declaring a root context, we return null because it would clash
|
||||
//with other classes, as there can only be a single root context.
|
||||
|
||||
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
//rootContext.register(RootApplicationConfig.class);
|
||||
//return rootContext;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WebApplicationContext createServletApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
secureWebAppContext.register(SecureWebAppConfig.class);
|
||||
return secureWebAppContext;
|
||||
AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
normalWebAppContext.register(NormalWebAppConfig.class);
|
||||
return normalWebAppContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/s/api/*" };
|
||||
return new String[] { "/api/*" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getServletName() {
|
||||
return "normal-dispatcher";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,11 @@ public class ApplicationInitializer implements WebApplicationInitializer {
|
|||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
//XML Context
|
||||
//Here, we can define a root context and register servlets, among other things.
|
||||
//However, since we've later defined other classes to do the same and they would clash,
|
||||
//we leave this commented out.
|
||||
|
||||
//Root XML Context
|
||||
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
|
||||
//rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml");
|
||||
//Annotations Context
|
||||
|
@ -24,12 +28,13 @@ public class ApplicationInitializer implements WebApplicationInitializer {
|
|||
//rootContext.register(RootApplicationConfig.class);
|
||||
//Registration
|
||||
//servletContext.addListener(new ContextLoaderListener(rootContext));
|
||||
|
||||
XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
|
||||
normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
|
||||
ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
|
||||
normal.setLoadOnStartup(1);
|
||||
normal.addMapping("/api/*");
|
||||
|
||||
//Dispatcher Servlet
|
||||
//XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
|
||||
//normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
|
||||
//ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
|
||||
//normal.setLoadOnStartup(1);
|
||||
//normal.addMapping("/api/*");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
||||
|
||||
public class SecureAnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WebApplicationContext createServletApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
secureWebAppContext.register(SecureWebAppConfig.class);
|
||||
return secureWebAppContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/s/api/*" };
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String getServletName() {
|
||||
return "secure-dispatcher";
|
||||
}
|
||||
|
||||
}
|
|
@ -35,7 +35,7 @@ public class HelloWorldController {
|
|||
@RequestMapping(path = "/welcome")
|
||||
public ModelAndView helloWorld() {
|
||||
processContext();
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3>Normal " + greeterService.greet() + "</h3></div>";
|
||||
return new ModelAndView("welcome", "message", message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public class HelloWorldSecureController {
|
|||
@RequestMapping(path = "/welcome")
|
||||
public ModelAndView helloWorld() {
|
||||
processContext();
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3>Secure " + greeterService.greet() + "</h3></div>";
|
||||
return new ModelAndView("welcome", "message", message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,10 +19,13 @@ public class StudentControllerConfig implements WebApplicationInitializer {
|
|||
|
||||
root.setServletContext(sc);
|
||||
|
||||
// Manages the lifecycle of the root application context
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
//Manages the lifecycle of the root application context.
|
||||
//Conflicts with other root contexts in the application, so we've manually set the parent below.
|
||||
//sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
DispatcherServlet dv = new DispatcherServlet(new GenericWebApplicationContext());
|
||||
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
|
||||
webApplicationContext.setParent(root);
|
||||
DispatcherServlet dv = new DispatcherServlet(webApplicationContext);
|
||||
|
||||
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
|
||||
appServlet.setLoadOnStartup(1);
|
||||
|
|
|
@ -26,11 +26,14 @@ public class MainWebAppInitializer implements WebApplicationInitializer {
|
|||
root.scan("org.baeldung.spring.config");
|
||||
// root.getEnvironment().setDefaultProfiles("embedded");
|
||||
|
||||
// Manages the lifecycle of the root application context
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
//Manages the lifecycle of the root application context.
|
||||
//Conflicts with other root contexts in the application, so we've manually set the parent below.
|
||||
//sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
// Handles requests into the application
|
||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
|
||||
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
|
||||
webApplicationContext.setParent(root);
|
||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(webApplicationContext));
|
||||
appServlet.setLoadOnStartup(1);
|
||||
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
||||
if (!mappingConflicts.isEmpty()) {
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.contexts.normal" />
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.contexts.secure" />
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
application initializers. -->
|
||||
<!--<absolute-ordering>
|
||||
</absolute-ordering>-->
|
||||
|
||||
<!-- load root application context -->
|
||||
|
||||
<!-- root application context -->
|
||||
<!--<listener>
|
||||
<listener-class>
|
||||
org.springframework.web.context.ContextLoaderListener
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
|
||||
>
|
||||
|
||||
<display-name>Spring MVC Application</display-name>
|
||||
|
||||
<!-- Spring root -->
|
||||
<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>
|
||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.spring.web.config</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
Loading…
Reference in New Issue