Skip to content

Commit 58612d0

Browse files
alessiostallaKevinGilmore
authored andcommitted
BAEL-82 (eugenp#4109)
* BAEL-82 revised example code * Fix the build
1 parent 86498b6 commit 58612d0

8 files changed

+115
-47
lines changed

spring-all/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<dependency>
171171
<groupId>org.springframework.boot</groupId>
172172
<artifactId>spring-boot-starter-thymeleaf</artifactId>
173+
<version>${org.springframework.version}</version>
173174
</dependency>
174175
</dependencies>
175176
</dependencyManagement>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.web.context.AbstractContextLoaderInitializer;
4+
import org.springframework.web.context.WebApplicationContext;
5+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
7+
8+
public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
9+
10+
@Override
11+
protected WebApplicationContext createRootApplicationContext() {
12+
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
13+
rootContext.register(RootApplicationConfig.class);
14+
return rootContext;
15+
}
16+
17+
@Override
18+
protected WebApplicationContext createServletApplicationContext() {
19+
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
20+
secureWebAppContext.register(SecureWebAppConfig.class);
21+
return secureWebAppContext;
22+
}
23+
24+
@Override
25+
protected String[] getServletMappings() {
26+
return new String[] { "/s/api/*" };
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.web.context.AbstractContextLoaderInitializer;
4+
import org.springframework.web.context.WebApplicationContext;
5+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
7+
public class AnnotationsBasedApplicationInitializer extends AbstractContextLoaderInitializer {
8+
9+
@Override
10+
protected WebApplicationContext createRootApplicationContext() {
11+
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
12+
rootContext.register(RootApplicationConfig.class);
13+
return rootContext;
14+
}
15+
16+
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
import javax.servlet.ServletRegistration;
66

77
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
89
import org.springframework.web.WebApplicationInitializer;
910
import org.springframework.web.context.ContextLoaderListener;
1011
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.context.support.XmlWebApplicationContext;
1113
import org.springframework.web.servlet.DispatcherServlet;
1214

13-
@Configuration
1415
public class ApplicationInitializer implements WebApplicationInitializer {
1516

1617
@Override
1718
public void onStartup(ServletContext servletContext) throws ServletException {
18-
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
19-
rootContext.register(RootApplicationConfig.class);
20-
servletContext.addListener(new ContextLoaderListener(rootContext));
19+
//XML Context
20+
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
21+
//rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml");
22+
//Annotations Context
23+
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
24+
//rootContext.register(RootApplicationConfig.class);
25+
//Registration
26+
//servletContext.addListener(new ContextLoaderListener(rootContext));
2127

22-
AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
23-
normalWebAppContext.register(NormalWebAppConfig.class);
28+
XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
29+
normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
2430
ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
2531
normal.setLoadOnStartup(1);
2632
normal.addMapping("/api/*");
27-
28-
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
29-
secureWebAppContext.register(SecureWebAppConfig.class);
30-
ServletRegistration.Dynamic secure = servletContext.addServlet("secure-webapp", new DispatcherServlet(secureWebAppContext));
31-
secure.setLoadOnStartup(1);
32-
secure.addMapping("/s/api/*");
3333
}
3434

3535
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version = "1.0" encoding = "UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7+
8+
<bean id="greeting" class="com.baeldung.contexts.Greeting">
9+
<property name="message" value="Hello World !!" />
10+
</bean>
11+
</beans>

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@
1010

1111
<context:component-scan base-package="com.baeldung.contexts.services" />
1212

13-
<bean id="greeting" class="ccom.baeldung.contexts.Greeting">
14-
<property name="message" value="Hello World !!" />
15-
</bean>
13+
<import resource="greeting.xml" />
1614
</beans>

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

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,54 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
55
version="3.0">
6+
7+
<!-- Uncommented, this disallows org.springframework.web.SpringServletContainerInitializer to run and execute
8+
application initializers. -->
9+
<!--<absolute-ordering>
10+
</absolute-ordering>-->
611

712
<!-- load root application context -->
8-
<context-param>
13+
<!--<listener>
14+
<listener-class>
15+
org.springframework.web.context.ContextLoaderListener
16+
</listener-class>
17+
</listener>-->
18+
<!--<context-param>
919
<param-name>contextConfigLocation</param-name>
1020
<param-value>/WEB-INF/rootApplicationContext.xml</param-value>
11-
</context-param>
12-
<listener>
13-
<listener-class>
14-
org.springframework.web.context.ContextLoaderListener
15-
</listener-class>
16-
</listener>
17-
21+
</context-param>-->
22+
<!--<context-param>
23+
<param-name>contextClass</param-name>
24+
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
25+
</context-param>
26+
<context-param>
27+
<param-name>contextConfigLocation</param-name>
28+
<param-value>com.baeldung.contexts.config.RootApplicationConfig, com.baeldung.contexts.config.NormalWebAppConfig</param-value>
29+
</context-param>-->
30+
<!--<context-param>
31+
<param-name>contextConfigLocation</param-name>
32+
<param-value>org.baeldung.bean.config</param-value>
33+
</context-param>-->
34+
1835
<!-- secure web app context -->
19-
<servlet>
36+
<!--<servlet>
2037
<servlet-name>secure-webapp</servlet-name>
21-
<servlet-class>
22-
org.springframework.web.servlet.DispatcherServlet
23-
</servlet-class>
24-
<load-on-startup>1</load-on-startup>
25-
<init-param>
26-
<param-name>contextConfigLocation</param-name>
27-
<param-value>/WEB-INF/secure-webapp-servlet.xml</param-value>
28-
</init-param>
38+
<servlet-class>
39+
org.springframework.web.servlet.DispatcherServlet
40+
</servlet-class>
41+
<init-param>
42+
<param-name>contextConfigLocation</param-name>
43+
<param-value>/WEB-INF/secure-webapp-servlet.xml</param-value>
44+
</init-param>
45+
<load-on-startup>1</load-on-startup>
2946
</servlet>
3047
<servlet-mapping>
3148
<servlet-name>secure-webapp</servlet-name>
3249
<url-pattern>/s/api/*</url-pattern>
33-
</servlet-mapping>
50+
</servlet-mapping>-->
3451

3552
<!-- normal web app context -->
36-
<servlet>
53+
<!--<servlet>
3754
<servlet-name>normal-webapp</servlet-name>
3855
<servlet-class>
3956
org.springframework.web.servlet.DispatcherServlet
@@ -43,23 +60,26 @@
4360
<servlet-mapping>
4461
<servlet-name>normal-webapp</servlet-name>
4562
<url-pattern>/api/*</url-pattern>
46-
</servlet-mapping>
47-
63+
</servlet-mapping>-->
64+
<!-- normal webapp with annotations-based context -->
4865
<servlet>
49-
<servlet-name>test-mvc</servlet-name>
66+
<servlet-name>normal-webapp-annotations</servlet-name>
5067
<servlet-class>
5168
org.springframework.web.servlet.DispatcherServlet
5269
</servlet-class>
70+
<init-param>
71+
<param-name>contextClass</param-name>
72+
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
73+
</init-param>
5374
<init-param>
5475
<param-name>contextConfigLocation</param-name>
55-
<param-value>/WEB-INF/test-mvc.xml</param-value>
76+
<param-value>com.baeldung.contexts.config.NormalWebAppConfig</param-value>
5677
</init-param>
5778
<load-on-startup>1</load-on-startup>
5879
</servlet>
59-
6080
<servlet-mapping>
61-
<servlet-name>test-mvc</servlet-name>
62-
<url-pattern>/test/*</url-pattern>
81+
<servlet-name>normal-webapp-annotations</servlet-name>
82+
<url-pattern>/api-ann/*</url-pattern>
6383
</servlet-mapping>
6484

6585
<welcome-file-list>

0 commit comments

Comments
 (0)