Merge branch '1.5.x' into 2.0.x

This commit is contained in:
Phillip Webb 2018-11-14 18:24:32 -08:00
commit 3baf782822
4 changed files with 60 additions and 12 deletions

View File

@ -90,8 +90,11 @@ public class AnnotationsPropertySource extends EnumerablePropertySource<Class<?>
if (annotations != null) { if (annotations != null) {
for (Annotation annotation : annotations) { for (Annotation annotation : annotations) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) { if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
mergedAnnotations Annotation mergedAnnotation = findMergedAnnotation(root,
.add(findMergedAnnotation(root, annotation.annotationType())); annotation.annotationType());
if (mergedAnnotation != null) {
mergedAnnotations.add(mergedAnnotation);
}
} }
} }
} }

View File

@ -34,7 +34,7 @@
</executions> </executions>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.example</groupid> <groupId>com.example</groupId>
<artifactId>custom-layout</artifactId> <artifactId>custom-layout</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version> <version>0.0.1.BUILD-SNAPSHOT</version>
</dependency> </dependency>

View File

@ -132,6 +132,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
beanFactory.addBeanPostProcessor( beanFactory.addBeanPostProcessor(
new WebApplicationContextServletContextAwareProcessor(this)); new WebApplicationContextServletContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(ServletContextAware.class); beanFactory.ignoreDependencyInterface(ServletContextAware.class);
registerWebApplicationScopes(null);
} }
@Override @Override
@ -226,19 +227,22 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
private void selfInitialize(ServletContext servletContext) throws ServletException { private void selfInitialize(ServletContext servletContext) throws ServletException {
prepareWebApplicationContext(servletContext); prepareWebApplicationContext(servletContext);
ConfigurableListableBeanFactory beanFactory = getBeanFactory(); registerWebApplicationScopes(servletContext);
ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes( WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),
beanFactory); servletContext);
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory,
getServletContext());
existingScopes.restore();
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory,
getServletContext());
for (ServletContextInitializer beans : getServletContextInitializerBeans()) { for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
beans.onStartup(servletContext); beans.onStartup(servletContext);
} }
} }
private void registerWebApplicationScopes(ServletContext servletContext) {
ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(
getBeanFactory());
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
servletContext);
existingScopes.restore();
}
/** /**
* Returns {@link ServletContextInitializer}s that should be used with the embedded * Returns {@link ServletContextInitializer}s that should be used with the embedded
* web server. By default this method will first attempt to find * web server. By default this method will first attempt to find

View File

@ -39,12 +39,15 @@ import org.mockito.Captor;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.Scope; import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer; import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean; import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
@ -449,7 +452,8 @@ public class ServletWebServerApplicationContextTests {
} }
@Test @Test
public void doesNotReplaceExistingScopes() { // gh-2082 public void doesNotReplaceExistingScopes() {
// gh-2082
Scope scope = mock(Scope.class); Scope scope = mock(Scope.class);
ConfigurableListableBeanFactory factory = this.context.getBeanFactory(); ConfigurableListableBeanFactory factory = this.context.getBeanFactory();
factory.registerScope(WebApplicationContext.SCOPE_REQUEST, scope); factory.registerScope(WebApplicationContext.SCOPE_REQUEST, scope);
@ -462,6 +466,29 @@ public class ServletWebServerApplicationContextTests {
.isSameAs(scope); .isSameAs(scope);
} }
@Test
public void servletRequestCanBeInjectedEarly() throws Exception {
// gh-14990
addWebServerFactoryBean();
RootBeanDefinition beanDefinition = new RootBeanDefinition(
WithAutowiredServletRequest.class);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
this.context.registerBeanDefinition("withAutowiredServletRequest",
beanDefinition);
this.context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
WithAutowiredServletRequest bean = beanFactory
.getBean(WithAutowiredServletRequest.class);
assertThat(bean.getRequest()).isNotNull();
}
});
this.context.refresh();
}
private void addWebServerFactoryBean() { private void addWebServerFactoryBean() {
this.context.registerBeanDefinition("webServerFactory", this.context.registerBeanDefinition("webServerFactory",
new RootBeanDefinition(MockServletWebServerFactory.class)); new RootBeanDefinition(MockServletWebServerFactory.class));
@ -514,4 +541,18 @@ public class ServletWebServerApplicationContextTests {
} }
protected static class WithAutowiredServletRequest {
private final ServletRequest request;
public WithAutowiredServletRequest(ServletRequest request) {
this.request = request;
}
public ServletRequest getRequest() {
return this.request;
}
}
} }