Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit aefa982

Browse files
committed
Merge pull request #119 from robinhh/master
Add repro project for SPR-9876
2 parents c2eda02 + b8e29d6 commit aefa982

File tree

11 files changed

+457
-0
lines changed

11 files changed

+457
-0
lines changed

SPR-13978/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Spring MVC project with Java config
2+
3+
This is a simple template for creating issue reproduction projects per
4+
the [README in the root of this repository](https://github.com/spring-projects/spring-framework-issues#readme).
5+
Please review that document before starting.
6+
7+
As described at the link above, do not edit this project directly! Rather
8+
use the `./create-repro-project.sh` script to create a fresh copy to
9+
a new directory having the same name as the JIRA issue you're trying
10+
to reproduce and edit from there.
11+
12+
## Deploying
13+
14+
It is possible to deploy your application directly from the command-line
15+
using maven. See the next two sections on Cargo and Jetty.
16+
17+
### Cargo
18+
19+
You can deploy with the [Cargo Maven plugin](http://cargo.codehaus.org/) which
20+
supports a wide [range of servers](http://cargo.codehaus.org/Containers).
21+
The required command is `mvn package cargo:run`.
22+
23+
By default Cargo is configured to start with `Tomcat 8` but you can easily
24+
edit the plugin settings in `pom.xml` to switch to a different server
25+
and version. The pom.xml or to switch to debug settings.
26+
27+
### Jetty
28+
29+
You can also deploy with the
30+
[Jetty Maven plugin](http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html).
31+
The required command is `mvn jetty:run` or `mvnDebug jetty:run`.
32+
33+
## Logging
34+
35+
This project contains a `log4j.properties` file in `src/main/resources` that you
36+
may wish to configure to emit more detailed logging. The root logger is set to
37+
`INFO` and a custom `org.springframework.web` logger is set to `DEBUG`.

SPR-13978/pom.xml

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.springframework.issues</groupId>
5+
<artifactId>SPR-13978</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<name>Spring MVC Issue Reproduction Project</name>
8+
<packaging>war</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<java.version>1.6</java.version>
13+
<spring.version>4.2.4.RELEASE</spring.version>
14+
<slf4j.version>1.7.12</slf4j.version>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- Spring Framework -->
19+
<dependency>
20+
<groupId>org.springframework</groupId>
21+
<artifactId>spring-context</artifactId>
22+
<version>${spring.version}</version>
23+
<exclusions>
24+
<!-- Exclude Commons Logging in favor of SLF4j -->
25+
<exclusion>
26+
<groupId>commons-logging</groupId>
27+
<artifactId>commons-logging</artifactId>
28+
</exclusion>
29+
</exclusions>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-webmvc</artifactId>
34+
<version>${spring.version}</version>
35+
</dependency>
36+
37+
<!-- Logging -->
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-api</artifactId>
41+
<version>${slf4j.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.slf4j</groupId>
45+
<artifactId>jcl-over-slf4j</artifactId>
46+
<version>${slf4j.version}</version>
47+
<scope>runtime</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-log4j12</artifactId>
52+
<version>${slf4j.version}</version>
53+
<scope>runtime</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>log4j</groupId>
57+
<artifactId>log4j</artifactId>
58+
<version>1.2.17</version>
59+
<scope>runtime</scope>
60+
</dependency>
61+
62+
<!-- Servlet API -->
63+
<dependency>
64+
<groupId>javax.servlet</groupId>
65+
<artifactId>javax.servlet-api</artifactId>
66+
<version>3.1.0</version>
67+
<scope>provided</scope>
68+
</dependency>
69+
70+
<!-- JSP API and JSTL
71+
<dependency>
72+
<groupId>javax.servlet.jsp</groupId>
73+
<artifactId>jsp-api</artifactId>
74+
<version>2.1</version>
75+
<scope>provided</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>javax.servlet</groupId>
79+
<artifactId>jstl</artifactId>
80+
<version>1.2</version>
81+
</dependency>
82+
-->
83+
84+
<!-- Test -->
85+
<dependency>
86+
<groupId>junit</groupId>
87+
<artifactId>junit</artifactId>
88+
<version>4.12</version>
89+
<scope>test</scope>
90+
</dependency>
91+
</dependencies>
92+
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-compiler-plugin</artifactId>
98+
<version>2.5.1</version>
99+
<configuration>
100+
<source>${java.version}</source>
101+
<target>${java.version}</target>
102+
</configuration>
103+
</plugin>
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-dependency-plugin</artifactId>
107+
<version>2.8</version>
108+
<executions>
109+
<execution>
110+
<id>install</id>
111+
<phase>install</phase>
112+
<goals>
113+
<goal>sources</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
<plugin>
119+
<groupId>org.apache.maven.plugins</groupId>
120+
<artifactId>maven-eclipse-plugin</artifactId>
121+
<version>2.8</version>
122+
<configuration>
123+
<downloadSources>true</downloadSources>
124+
<downloadJavadocs>false</downloadJavadocs>
125+
<wtpversion>2.0</wtpversion>
126+
</configuration>
127+
</plugin>
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-surefire-plugin</artifactId>
131+
<version>2.12.4</version>
132+
<configuration>
133+
<includes>
134+
<include>**/*Tests.java</include>
135+
<include>**/*Test.java</include>
136+
</includes>
137+
<excludes>
138+
<exclude>**/*Abstract*.java</exclude>
139+
</excludes>
140+
</configuration>
141+
</plugin>
142+
<plugin>
143+
<groupId>org.eclipse.jetty</groupId>
144+
<artifactId>jetty-maven-plugin</artifactId>
145+
<version>9.3.3.v20150827</version>
146+
</plugin>
147+
<plugin>
148+
<groupId>org.codehaus.cargo</groupId>
149+
<artifactId>cargo-maven2-plugin</artifactId>
150+
<version>1.4.7</version>
151+
<configuration>
152+
<configuration>
153+
<properties>
154+
<cargo.servlet.port>8080</cargo.servlet.port>
155+
<cargo.logging>medium</cargo.logging>
156+
<cargo.jvmargs>-Xms96m -Xmx512m -Djava.awt.headless=true</cargo.jvmargs>
157+
<!--
158+
<cargo.jvmargs>
159+
-Xdebug
160+
-Xrunjdwp:transport=dt_socket,address=8000,suspend=n,server=y
161+
-Xnoagent
162+
-Djava.compiler=NONE
163+
</cargo.jvmargs>
164+
-->
165+
</properties>
166+
</configuration>
167+
<container>
168+
<containerId>tomcat8x</containerId>
169+
<zipUrlInstaller>
170+
<url>http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.zip</url>
171+
</zipUrlInstaller>
172+
</container>
173+
</configuration>
174+
</plugin>
175+
</plugins>
176+
</build>
177+
178+
<repositories>
179+
<repository>
180+
<id>spring-maven-snapshot</id>
181+
<name>Springframework Maven Snapshot Repository</name>
182+
<url>http://repo.spring.io/snapshot</url>
183+
<snapshots>
184+
<enabled>true</enabled>
185+
</snapshots>
186+
</repository>
187+
</repositories>
188+
189+
</project>
190+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.springframework.issues.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.context.ConfigurableWebApplicationContext;
6+
import org.springframework.web.context.ContextLoaderListener;
7+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
8+
import org.springframework.web.servlet.DispatcherServlet;
9+
import org.springframework.web.util.HttpSessionMutexListener;
10+
11+
import javax.servlet.ServletContext;
12+
import javax.servlet.ServletException;
13+
import javax.servlet.ServletRegistration;
14+
15+
16+
public class BootstrapSpringDirectly { //} implements WebApplicationInitializer {
17+
18+
static final String DISPATCHER_SERVLET_NAME = "SpringMVCDispatcher";
19+
static final Logger logger = LoggerFactory.getLogger(BootstrapSpringDirectly.class);
20+
21+
//@Override
22+
public void onStartup(ServletContext servletContext) throws ServletException {
23+
24+
logger.info("===============================================================================");
25+
logger.info("== starting up ... ==");
26+
logger.info("===============================================================================");
27+
28+
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
29+
30+
// XXX load everything here in order to have all our beans in one application context:
31+
rootContext.register(WebConfig.class); // DispatcherServlet creates another (empty) context which inherits from this application context
32+
33+
servletContext.addListener(new ContextLoaderListener(rootContext));
34+
servletContext.addListener(new HttpSessionMutexListener());
35+
servletContext.addListener(new StartupListener());
36+
37+
// register and map the dispatcher servlet:
38+
39+
ConfigurableWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); // XXX do we need a child context?
40+
dispatcherContext.setParent(rootContext);
41+
DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
42+
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); // we don't use a default handler but handle that exception in a controller advice (GlobalExceptionHandler)
43+
ServletRegistration.Dynamic registration = servletContext.addServlet(DISPATCHER_SERVLET_NAME, dispatcherServlet);
44+
registration.setLoadOnStartup(1);
45+
registration.addMapping("/");
46+
registration.setAsyncSupported(true);
47+
48+
49+
logger.info("... bootstrap code done.");
50+
}
51+
52+
53+
}
54+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.springframework.issues.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.context.WebApplicationContext;
6+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
7+
import org.springframework.web.servlet.DispatcherServlet;
8+
import org.springframework.web.servlet.FrameworkServlet;
9+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
10+
import org.springframework.web.util.HttpSessionMutexListener;
11+
12+
import javax.servlet.ServletContext;
13+
import javax.servlet.ServletException;
14+
15+
16+
public class BootstrapSpringUsingInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(BootstrapSpringUsingInitializer.class);
19+
20+
21+
@Override protected WebApplicationContext createServletApplicationContext() {
22+
return new AnnotationConfigWebApplicationContext();
23+
}
24+
25+
@Override protected String[] getServletMappings() {
26+
return new String[]{ "/" };
27+
}
28+
29+
@Override protected Class<?>[] getRootConfigClasses() {
30+
return null;
31+
// XXX this causes an IllegalStateException when WebApplicationContextUtils.getRequiredWebApplicationContext() is called from StartupListener
32+
}
33+
34+
@Override protected Class<?>[] getServletConfigClasses() {
35+
return new Class<?>[]{ WebConfig.class };
36+
}
37+
38+
@Override protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
39+
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
40+
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); // we don't use a default handler but handle that exception in a controller advice (GlobalExceptionHandler)
41+
return dispatcherServlet;
42+
}
43+
44+
@Override public void onStartup(ServletContext servletContext) throws ServletException {
45+
logger.info("===============================================================================");
46+
logger.info("== starting up ... ==");
47+
logger.info("===============================================================================");
48+
49+
super.onStartup(servletContext);
50+
51+
servletContext.addListener(new HttpSessionMutexListener());
52+
servletContext.addListener(new StartupListener());
53+
54+
logger.info("... bootstrap code done.");
55+
}
56+
57+
58+
}
59+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.springframework.issues.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.context.WebApplicationContext;
6+
import org.springframework.web.context.support.WebApplicationContextUtils;
7+
8+
import javax.servlet.ServletContext;
9+
import javax.servlet.ServletContextEvent;
10+
import javax.servlet.ServletContextListener;
11+
12+
13+
public class StartupListener implements ServletContextListener {
14+
15+
private static final Logger logger = LoggerFactory.getLogger(StartupListener.class);
16+
17+
@Override @SuppressWarnings("HardCodedStringLiteral")
18+
public void contextInitialized(ServletContextEvent event) {
19+
ServletContext servletContext = event.getServletContext();
20+
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
21+
22+
// in a real application springContext is used to get access to some beans
23+
24+
logger.info("===============================================================================");
25+
logger.info("== startup done.");
26+
logger.info("===============================================================================");
27+
28+
servletContext.log("==> startup done <==");
29+
}
30+
31+
@Override
32+
public void contextDestroyed(ServletContextEvent event) {
33+
}
34+
35+
}

0 commit comments

Comments
 (0)