From b875a00ebd15fbfcd9e72bffc9ca75f0c635da24 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Jan 2015 17:26:47 +0000 Subject: [PATCH] Tighten up conditions: a web application may not be using Spring MVC Previously, some classes that were annotatated with @ConditionalOnWebApplication assumed that, if the application was a web application, Spring MVC (spring-webmvc) would be on the classpath. If it was not, the application would fail to start, typically with an error relating to WebMvcConfigurerAdapter being unavailable. This commit updates the affected configuration classes and annotates them with @ConditionalOnClass(WebMvcConfigurerAdapter.class) to ensure that their auto-configuration only takes effect if its a web application and Spring MVC is on the classpath. Fixes gh-2345 --- .../jpa/JpaRepositoriesAutoConfiguration.java | 4 ++- .../orm/jpa/JpaBaseConfiguration.java | 26 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java index 22dffde4a72..3c599f60ce4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2015 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. @@ -33,6 +33,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.data.web.config.EnableSpringDataWebSupport; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JPA Repositories. @@ -64,6 +65,7 @@ public class JpaRepositoriesAutoConfiguration { @Configuration @EnableSpringDataWebSupport + @ConditionalOnClass(WebMvcConfigurerAdapter.class) @ConditionalOnWebApplication @ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class) protected static class JpaWebConfiguration { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java index cf8dfeaee05..eb8a962eeb2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; @@ -134,19 +135,26 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { @Configuration @ConditionalOnWebApplication + @ConditionalOnClass(WebMvcConfigurerAdapter.class) @ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class, OpenEntityManagerInViewFilter.class }) @ConditionalOnExpression("${spring.jpa.openInView:${spring.jpa.open_in_view:true}}") - protected static class JpaWebConfiguration extends WebMvcConfigurerAdapter { + protected static class JpaWebConfiguration { - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor()); - } + // Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when + // not on the classpath + @Configuration + protected static class JpaWebMvcConfiguration extends WebMvcConfigurerAdapter { - @Bean - public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { - return new OpenEntityManagerInViewInterceptor(); + @Bean + public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { + return new OpenEntityManagerInViewInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor()); + } } }