From 4f86233ca72171d6adb55369c08f8978c657af07 Mon Sep 17 00:00:00 2001 From: Jeffrey van der Laan Date: Sun, 10 Feb 2019 22:11:34 +0100 Subject: [PATCH] Do not auto-configure OEMIV filter when user defines registration bean See gh-15889 --- .../orm/jpa/JpaBaseConfiguration.java | 4 +- .../AbstractJpaAutoConfigurationTests.java | 56 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java index 56a0369375..073c06087b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -39,6 +39,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.domain.EntityScanPackages; import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; +import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; @@ -211,6 +212,7 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { @ConditionalOnClass(WebMvcConfigurer.class) @ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class, OpenEntityManagerInViewFilter.class }) + @ConditionalOnMissingFilterBean(OpenEntityManagerInViewFilter.class) @ConditionalOnProperty(prefix = "spring.jpa", name = "open-in-view", havingValue = "true", matchIfMissing = true) protected static class JpaWebConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java index cbca678798..637c08118a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 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. @@ -38,6 +38,7 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -159,6 +160,32 @@ public abstract class AbstractJpaAutoConfigurationTests { .doesNotHaveBean(OpenEntityManagerInViewInterceptor.class)); } + @Test + public void openEntityManagerInViewInterceptorIsNotRegisteredWhenFilterRegistrationPresent() { + new WebApplicationContextRunner() + .withPropertyValues("spring.datasource.generate-unique-name=true") + .withUserConfiguration(TestFilterRegistrationConfiguration.class) + .withConfiguration(AutoConfigurations.of( + DataSourceAutoConfiguration.class, + TransactionAutoConfiguration.class, this.autoConfiguredClass)) + .run((context) -> assertThat(context) + .doesNotHaveBean(OpenEntityManagerInViewInterceptor.class)); + } + + @Test + public void openEntityManagerInViewInterceptorAutoConfigurationBacksOffWhenManuallyRegistered() { + new WebApplicationContextRunner() + .withPropertyValues("spring.datasource.generate-unique-name=true") + .withUserConfiguration(TestInterceptorManualConfiguration.class) + .withConfiguration(AutoConfigurations.of( + DataSourceAutoConfiguration.class, + TransactionAutoConfiguration.class, this.autoConfiguredClass)) + .run((context) -> assertThat(context) + .getBean(OpenEntityManagerInViewInterceptor.class) + .isExactlyInstanceOf( + TestInterceptorManualConfiguration.ManualOpenEntityManagerInViewInterceptor.class)); + } + @Test public void openEntityManagerInViewInterceptorISNotRegisteredWhenExplicitlyOff() { new WebApplicationContextRunner() @@ -298,6 +325,33 @@ public abstract class AbstractJpaAutoConfigurationTests { } + @Configuration + @TestAutoConfigurationPackage(City.class) + protected static class TestFilterRegistrationConfiguration { + + @Bean + public FilterRegistrationBean OpenEntityManagerInViewFilterFilterRegistrationBean() { + return new FilterRegistrationBean<>(); + } + + } + + @Configuration + @TestAutoConfigurationPackage(City.class) + protected static class TestInterceptorManualConfiguration { + + @Bean + public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { + return new ManualOpenEntityManagerInViewInterceptor(); + } + + protected static class ManualOpenEntityManagerInViewInterceptor + extends OpenEntityManagerInViewInterceptor { + + } + + } + @Configuration protected static class TestConfigurationWithLocalContainerEntityManagerFactoryBean extends TestConfiguration {