提交 ab5f1256 编写于 作者: R Rossen Stoyanchev

Allow consolidating config in root context with Java

This change makes it possible to provide no configuration for the
DispatcherServlet when extending
AbstractAnnotationConfigDispatcherServletInitializer, and therefore
provide all config through the "root" context.

Issue: SPR-11357
上级 b6073d9a
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -50,8 +50,7 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer ...@@ -50,8 +50,7 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
protected WebApplicationContext createRootApplicationContext() { protected WebApplicationContext createRootApplicationContext() {
Class<?>[] rootConfigClasses = this.getRootConfigClasses(); Class<?>[] rootConfigClasses = this.getRootConfigClasses();
if (!ObjectUtils.isEmpty(rootConfigClasses)) { if (!ObjectUtils.isEmpty(rootConfigClasses)) {
AnnotationConfigWebApplicationContext rootAppContext = AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
new AnnotationConfigWebApplicationContext();
rootAppContext.register(rootConfigClasses); rootAppContext.register(rootConfigClasses);
return rootAppContext; return rootAppContext;
} }
...@@ -64,18 +63,14 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer ...@@ -64,18 +63,14 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
* {@inheritDoc} * {@inheritDoc}
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
* providing it the annotated classes returned by {@link #getServletConfigClasses()}. * providing it the annotated classes returned by {@link #getServletConfigClasses()}.
* @throws IllegalArgumentException if {@link #getServletConfigClasses()} returns
* empty or {@code null}
*/ */
@Override @Override
protected WebApplicationContext createServletApplicationContext() { protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext servletAppContext = AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
new AnnotationConfigWebApplicationContext(); Class<?>[] configClasses = this.getServletConfigClasses();
Class<?>[] servletConfigClasses = this.getServletConfigClasses(); if (!ObjectUtils.isEmpty(configClasses)) {
Assert.notEmpty(servletConfigClasses, servletAppContext.register(configClasses);
"getServletConfigClasses() did not return any configuration classes"); }
servletAppContext.register(servletConfigClasses);
return servletAppContext; return servletAppContext;
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,22 +21,16 @@ import static org.junit.Assert.assertFalse; ...@@ -21,22 +21,16 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Collections; import java.util.*;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.DispatcherType; import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration.Dynamic; import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.test.MockServletConfig;
import org.springframework.mock.web.test.MockServletContext; import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
...@@ -89,11 +83,11 @@ public class AnnotationConfigDispatcherServletInitializerTests { ...@@ -89,11 +83,11 @@ public class AnnotationConfigDispatcherServletInitializerTests {
assertNotNull(servlets.get(SERVLET_NAME)); assertNotNull(servlets.get(SERVLET_NAME));
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
WebApplicationContext dispatcherServletContext = servlet.getWebApplicationContext(); WebApplicationContext wac = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) dispatcherServletContext).refresh(); ((AnnotationConfigWebApplicationContext) wac).refresh();
assertTrue(dispatcherServletContext.containsBean("bean")); assertTrue(wac.containsBean("bean"));
assertTrue(dispatcherServletContext.getBean("bean") instanceof MyBean); assertTrue(wac.getBean("bean") instanceof MyBean);
assertEquals(1, servletRegistrations.size()); assertEquals(1, servletRegistrations.size());
assertNotNull(servletRegistrations.get(SERVLET_NAME)); assertNotNull(servletRegistrations.get(SERVLET_NAME));
...@@ -135,6 +129,33 @@ public class AnnotationConfigDispatcherServletInitializerTests { ...@@ -135,6 +129,33 @@ public class AnnotationConfigDispatcherServletInitializerTests {
filterRegistration.getMappings().get(SERVLET_NAME)); 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 @Test
public void noFilters() throws ServletException { public void noFilters() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() { initializer = new MyAnnotationConfigDispatcherServletInitializer() {
...@@ -167,6 +188,13 @@ public class AnnotationConfigDispatcherServletInitializerTests { ...@@ -167,6 +188,13 @@ public class AnnotationConfigDispatcherServletInitializerTests {
filterRegistrations.put(filterName, registration); filterRegistrations.put(filterName, registration);
return registration; return registration;
} }
@Override
public <T extends EventListener> void addListener(T t) {
if (t instanceof ServletContextListener) {
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
}
}
} }
private static class MyAnnotationConfigDispatcherServletInitializer private static class MyAnnotationConfigDispatcherServletInitializer
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册