Skip to content

Commit e2fbaa8

Browse files
committed
Merge pull request #62 from poutsma/SPR-9300
* SPR-9300: Add convenient WebAppInitializer base classes
2 parents 37e024c + f64c13a commit e2fbaa8

File tree

9 files changed

+794
-2
lines changed

9 files changed

+794
-2
lines changed

spring-web/src/main/java/org/springframework/web/WebApplicationInitializer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,6 +72,9 @@
7272
*
7373
* }</pre>
7474
*
75+
* As an alternative to the above, you can also extend from {@link
76+
* org.springframework.web.servlet.support.AbstractDispatcherServletInitializer}.
77+
*
7578
* As you can see, thanks to Servlet 3.0's new {@link ServletContext#addServlet} method
7679
* we're actually registering an <em>instance</em> of the {@code DispatcherServlet}, and
7780
* this means that the {@code DispatcherServlet} can now be treated like any other object
@@ -82,7 +85,7 @@
8285
* are free to create and work with your Spring application contexts as necessary before
8386
* injecting them into the {@code DispatcherServlet}.
8487
*
85-
* <p>Most major Spring Web componentry has been updated to support this style of
88+
* <p>Most major Spring Web components have been updated to support this style of
8689
* registration. You'll find that {@code DispatcherServlet}, {@code FrameworkServlet},
8790
* {@code ContextLoaderListener} and {@code DelegatingFilterProxy} all now support
8891
* constructor arguments. Even if a component (e.g. non-Spring, other third party) has not
@@ -131,6 +134,9 @@
131134
*
132135
* }</pre>
133136
*
137+
* As an alternative to the above, you can also extend from {@link
138+
* org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer}.
139+
*
134140
* Remember that {@code WebApplicationInitializer} implementations are <em>detected
135141
* automatically</em> -- so you are free to package them within your application as you
136142
* see fit.
@@ -165,6 +171,9 @@
165171
* @author Chris Beams
166172
* @since 3.1
167173
* @see SpringServletContainerInitializer
174+
* @see org.springframework.web.context.AbstractContextLoaderInitializer
175+
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer
176+
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
168177
*/
169178
public interface WebApplicationInitializer {
170179

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context;
18+
19+
import javax.servlet.ServletContext;
20+
import javax.servlet.ServletException;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
import org.springframework.web.WebApplicationInitializer;
26+
27+
/**
28+
* Convenient base class for {@link WebApplicationInitializer} implementations that
29+
* register a {@link ContextLoaderListener} in the servlet context.
30+
*
31+
* <p>The only method required to be implemented by subclasses is {@link
32+
* #createRootApplicationContext()}, which gets invoked from {@link
33+
* #registerContextLoaderListener(javax.servlet.ServletContext)}.
34+
*
35+
* @author Arjen Poutsma
36+
* @author Chris Beams
37+
* @since 3.2
38+
*/
39+
public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer {
40+
41+
/** Logger available to subclasses. */
42+
protected final Log logger = LogFactory.getLog(getClass());
43+
44+
public void onStartup(ServletContext servletContext) throws ServletException {
45+
this.registerContextLoaderListener(servletContext);
46+
}
47+
48+
/**
49+
* Register a {@link ContextLoaderListener} against the given servlet context. The
50+
* {@code ContextLoaderListener} is initialized with the application context returned
51+
* from the {@link #createRootApplicationContext()} template method.
52+
* @param servletContext the servlet context to register the listener against
53+
*/
54+
protected void registerContextLoaderListener(ServletContext servletContext) {
55+
WebApplicationContext rootAppContext = this.createRootApplicationContext();
56+
if (rootAppContext != null) {
57+
servletContext.addListener(new ContextLoaderListener(rootAppContext));
58+
}
59+
else {
60+
logger.debug("No ContextLoaderListener registered, as " +
61+
"createRootApplicationContext() did not return an application context");
62+
}
63+
}
64+
65+
/**
66+
* Create the "root" application context to be provided to the
67+
* {@code ContextLoaderListener}.
68+
* <p>The returned context is delegated to
69+
* {@link ContextLoaderListener#ContextLoaderListener(WebApplicationContext)} and will
70+
* be established as the parent context for any {@code DispatcherServlet} application
71+
* contexts. As such, it typically contains middle-tier services, data sources, etc.
72+
* @return the root application context, or {@code null} if a root context is not
73+
* desired
74+
* @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer
75+
*/
76+
protected abstract WebApplicationContext createRootApplicationContext();
77+
78+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context;
18+
19+
import java.util.EventListener;
20+
21+
import javax.servlet.ServletContextEvent;
22+
import javax.servlet.ServletException;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import org.springframework.mock.web.MockServletContext;
28+
import org.springframework.web.context.support.StaticWebApplicationContext;
29+
import org.springframework.web.context.support.WebApplicationContextUtils;
30+
31+
import static org.junit.Assert.*;
32+
33+
/**
34+
* Test case for {@link AbstractContextLoaderInitializer}.
35+
*
36+
* @author Arjen Poutsma
37+
*/
38+
public class ContextLoaderInitializerTests {
39+
40+
private static final String BEAN_NAME = "myBean";
41+
42+
private AbstractContextLoaderInitializer initializer;
43+
44+
private MockServletContext servletContext;
45+
46+
private EventListener eventListener;
47+
48+
@Before
49+
public void setUp() throws Exception {
50+
servletContext = new MyMockServletContext();
51+
initializer = new MyContextLoaderInitializer();
52+
eventListener = null;
53+
}
54+
55+
@Test
56+
public void register() throws ServletException {
57+
initializer.onStartup(servletContext);
58+
59+
assertTrue(eventListener instanceof ContextLoaderListener);
60+
ContextLoaderListener cll = (ContextLoaderListener) eventListener;
61+
cll.contextInitialized(new ServletContextEvent(servletContext));
62+
63+
WebApplicationContext applicationContext = WebApplicationContextUtils
64+
.getRequiredWebApplicationContext(servletContext);
65+
66+
assertTrue(applicationContext.containsBean(BEAN_NAME));
67+
assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean);
68+
}
69+
70+
private class MyMockServletContext extends MockServletContext {
71+
72+
@Override
73+
public <T extends EventListener> void addListener(T listener) {
74+
eventListener = listener;
75+
}
76+
77+
}
78+
79+
private static class MyContextLoaderInitializer
80+
extends AbstractContextLoaderInitializer {
81+
82+
@Override
83+
protected WebApplicationContext createRootApplicationContext() {
84+
StaticWebApplicationContext rootContext = new StaticWebApplicationContext();
85+
rootContext.registerSingleton(BEAN_NAME, MyBean.class);
86+
return rootContext;
87+
}
88+
}
89+
90+
private static class MyBean {
91+
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.support;
18+
19+
import org.springframework.util.Assert;
20+
import org.springframework.util.ObjectUtils;
21+
import org.springframework.web.context.WebApplicationContext;
22+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
23+
24+
/**
25+
* Base class for {@link org.springframework.web.WebApplicationInitializer
26+
* WebApplicationInitializer} implementations that register a {@link
27+
* org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
28+
* configured with annotated classes, e.g. Spring's {@link
29+
* org.springframework.context.annotation.Configuration @Configuration} classes.
30+
*
31+
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()},
32+
* {@link #getServletConfigClasses()}, as well as {@link #getServletMappings()}. Further
33+
* template and customization methods are provided by {@link
34+
* AbstractDispatcherServletInitializer}.
35+
*
36+
* @author Arjen Poutsma
37+
* @author Chris Beams
38+
* @since 3.2
39+
*/
40+
public abstract class AbstractAnnotationConfigDispatcherServletInitializer
41+
extends AbstractDispatcherServletInitializer {
42+
43+
/**
44+
* {@inheritDoc}
45+
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
46+
* providing it the annotated classes returned by {@link #getRootConfigClasses()}.
47+
* Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}.
48+
*/
49+
@Override
50+
protected WebApplicationContext createRootApplicationContext() {
51+
Class<?>[] rootConfigClasses = this.getRootConfigClasses();
52+
if (!ObjectUtils.isEmpty(rootConfigClasses)) {
53+
AnnotationConfigWebApplicationContext rootAppContext =
54+
new AnnotationConfigWebApplicationContext();
55+
rootAppContext.register(rootConfigClasses);
56+
return rootAppContext;
57+
}
58+
else {
59+
return null;
60+
}
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
66+
* providing it the annotated classes returned by {@link #getServletConfigClasses()}.
67+
* @throws IllegalArgumentException if {@link #getServletConfigClasses()} returns
68+
* empty or {@code null}
69+
*/
70+
@Override
71+
protected WebApplicationContext createServletApplicationContext() {
72+
AnnotationConfigWebApplicationContext servletAppContext =
73+
new AnnotationConfigWebApplicationContext();
74+
Class<?>[] servletConfigClasses = this.getServletConfigClasses();
75+
Assert.notEmpty(servletConfigClasses,
76+
"getServletConfigClasses() did not return any configuration classes");
77+
78+
servletAppContext.register(servletConfigClasses);
79+
return servletAppContext;
80+
}
81+
82+
/**
83+
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
84+
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
85+
* provided to the {@linkplain #createRootApplicationContext() root application context}.
86+
* @return the configuration classes for the root application context, or {@code null}
87+
* if creation and registration of a root context is not desired
88+
*/
89+
protected abstract Class<?>[] getRootConfigClasses();
90+
91+
/**
92+
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
93+
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
94+
* provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
95+
* application context}.
96+
* @return the configuration classes for the dispatcher servlet application context
97+
* (may not be empty or {@code null}).
98+
*/
99+
protected abstract Class<?>[] getServletConfigClasses();
100+
101+
}

0 commit comments

Comments
 (0)