Allow consolidating config in root context with Java

Issue: SPR-11357
(backported from 4.0.1)
This commit is contained in:
Juergen Hoeller
2014-01-24 17:46:31 +01:00
parent edb660863b
commit 9845d9eb21
2 changed files with 59 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.web.servlet.support;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -28,8 +27,8 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* configured with annotated classes, e.g. Spring's {@link
* org.springframework.context.annotation.Configuration @Configuration} classes.
*
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()},
* {@link #getServletConfigClasses()}, as well as {@link #getServletMappings()}. Further
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. Further
* template and customization methods are provided by {@link
* AbstractDispatcherServletInitializer}.
*
@@ -48,11 +47,10 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
*/
@Override
protected WebApplicationContext createRootApplicationContext() {
Class<?>[] rootConfigClasses = this.getRootConfigClasses();
if (!ObjectUtils.isEmpty(rootConfigClasses)) {
AnnotationConfigWebApplicationContext rootAppContext =
new AnnotationConfigWebApplicationContext();
rootAppContext.register(rootConfigClasses);
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(configClasses);
return rootAppContext;
}
else {
@@ -64,18 +62,14 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
* {@inheritDoc}
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
* providing it the annotated classes returned by {@link #getServletConfigClasses()}.
* @throws IllegalArgumentException if {@link #getServletConfigClasses()} returns
* empty or {@code null}
*/
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext servletAppContext =
new AnnotationConfigWebApplicationContext();
Class<?>[] servletConfigClasses = this.getServletConfigClasses();
Assert.notEmpty(servletConfigClasses,
"getServletConfigClasses() did not return any configuration classes");
servletAppContext.register(servletConfigClasses);
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
Class<?>[] configClasses = getServletConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
servletAppContext.register(configClasses);
}
return servletAppContext;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,33 +16,34 @@
package org.springframework.web.servlet.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.EnumSet;
import java.util.EventListener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.Servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.test.MockServletConfig;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.*;
/**
* Test case for {@link AbstractAnnotationConfigDispatcherServletInitializer}.
*
@@ -89,11 +90,11 @@ public class AnnotationConfigDispatcherServletInitializerTests {
assertNotNull(servlets.get(SERVLET_NAME));
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
WebApplicationContext dispatcherServletContext = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) dispatcherServletContext).refresh();
WebApplicationContext wac = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) wac).refresh();
assertTrue(dispatcherServletContext.containsBean("bean"));
assertTrue(dispatcherServletContext.getBean("bean") instanceof MyBean);
assertTrue(wac.containsBean("bean"));
assertTrue(wac.getBean("bean") instanceof MyBean);
assertEquals(1, servletRegistrations.size());
assertNotNull(servletRegistrations.get(SERVLET_NAME));
@@ -135,6 +136,32 @@ public class AnnotationConfigDispatcherServletInitializerTests {
filterRegistration.getMappings().get(SERVLET_NAME));
}
// SPR-11357
@Test
public void rootContextOnly() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {MyConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
};
initializer.onStartup(servletContext);
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
servlet.init(new MockServletConfig(this.servletContext));
WebApplicationContext wac = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) wac).refresh();
assertTrue(wac.containsBean("bean"));
assertTrue(wac.getBean("bean") instanceof MyBean);
}
@Test
public void noFilters() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@@ -152,6 +179,12 @@ public class AnnotationConfigDispatcherServletInitializerTests {
private class MyMockServletContext extends MockServletContext {
public <T extends EventListener> void addListener(T t) {
if (t instanceof ServletContextListener) {
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
}
}
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
servlets.put(servletName, servlet);
@@ -169,6 +202,7 @@ public class AnnotationConfigDispatcherServletInitializerTests {
}
}
private static class MyAnnotationConfigDispatcherServletInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@@ -201,13 +235,13 @@ public class AnnotationConfigDispatcherServletInitializerTests {
protected Class<?>[] getRootConfigClasses() {
return null;
}
}
private static class MyBean {
}
@Configuration
@SuppressWarnings("unused")
private static class MyConfiguration {
@@ -219,7 +253,6 @@ public class AnnotationConfigDispatcherServletInitializerTests {
public MyBean bean() {
return new MyBean();
}
}
}