From 274098f2e2413034e2538bc937c22180e2deaabe Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Mon, 29 Jul 2019 09:27:10 +0200 Subject: [PATCH 1/3] Refine back-off strategy of Neo4j SessionFactory This commit separates the auto-configuration of the `SessionFactory` in an isolated class so that the rest of the auto-configuration is still applied if the user provides a custom `SessionFactory` bean. See gh-17662 --- .../neo4j/Neo4jDataAutoConfiguration.java | 46 +++++++++++-------- .../Neo4jDataAutoConfigurationTests.java | 8 ++++ 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java index 362245c6ec..a70a4df177 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java @@ -53,29 +53,15 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * @author Vince Bickers * @author Stephane Nicoll * @author Kazuki Shimizu + * @author Michael Simons * @since 1.4.0 */ @Configuration @ConditionalOnClass({ SessionFactory.class, Neo4jTransactionManager.class, PlatformTransactionManager.class }) -@ConditionalOnMissingBean(SessionFactory.class) @EnableConfigurationProperties(Neo4jProperties.class) @Import(Neo4jBookmarkManagementConfiguration.class) public class Neo4jDataAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public org.neo4j.ogm.config.Configuration configuration(Neo4jProperties properties) { - return properties.createConfiguration(); - } - - @Bean - public SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration, - ApplicationContext applicationContext, ObjectProvider eventListeners) { - SessionFactory sessionFactory = new SessionFactory(configuration, getPackagesToScan(applicationContext)); - eventListeners.stream().forEach(sessionFactory::register); - return sessionFactory; - } - @Bean @ConditionalOnMissingBean(PlatformTransactionManager.class) public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, Neo4jProperties properties, @@ -91,12 +77,32 @@ public class Neo4jDataAutoConfiguration { return transactionManager; } - private String[] getPackagesToScan(ApplicationContext applicationContext) { - List packages = EntityScanPackages.get(applicationContext).getPackageNames(); - if (packages.isEmpty() && AutoConfigurationPackages.has(applicationContext)) { - packages = AutoConfigurationPackages.get(applicationContext); + @Configuration + @ConditionalOnMissingBean(SessionFactory.class) + protected static class Neo4jOgmSessionFactoryConfiguration { + + @Bean + @ConditionalOnMissingBean + public org.neo4j.ogm.config.Configuration configuration(Neo4jProperties properties) { + return properties.createConfiguration(); + } + + @Bean + public SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration, + ApplicationContext applicationContext, ObjectProvider eventListeners) { + SessionFactory sessionFactory = new SessionFactory(configuration, getPackagesToScan(applicationContext)); + eventListeners.stream().forEach(sessionFactory::register); + return sessionFactory; } - return StringUtils.toStringArray(packages); + + private String[] getPackagesToScan(ApplicationContext applicationContext) { + List packages = EntityScanPackages.get(applicationContext).getPackageNames(); + if (packages.isEmpty() && AutoConfigurationPackages.has(applicationContext)) { + packages = AutoConfigurationPackages.get(applicationContext); + } + return StringUtils.toStringArray(packages); + } + } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java index 260aa841fc..d2ee45e57c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java @@ -97,6 +97,14 @@ public class Neo4jDataAutoConfigurationTests { }); } + @Test + public void customSessionFactoryShouldNotDisableOtherDefaults() { + this.contextRunner.withUserConfiguration(CustomSessionFactory.class).run((context) -> { + assertThat(context).hasSingleBean(Neo4jTransactionManager.class); + assertThat(context).hasSingleBean(OpenSessionInViewInterceptor.class); + }); + } + @Test public void customConfiguration() { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { From bacb4d1715dd0add5408fc3b4368a449c434fa1d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 2 Aug 2019 17:12:27 +0200 Subject: [PATCH 2/3] Polish "Refine back-off strategy of Neo4j SessionFactory" See gh-17662 --- .../src/main/asciidoc/spring-boot-features.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 938b5cf74b..1d778b1f7a 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -4385,9 +4385,9 @@ properties, as shown in the following example: spring.data.neo4j.password=secret ---- -You can take full control over the session creation by adding a -`org.neo4j.ogm.config.Configuration` `@Bean`. Also, adding a `@Bean` of type -`SessionFactory` disables the auto-configuration and gives you full control. +You can take full control over the session creation by either adding a +`org.neo4j.ogm.config.Configuration` bean or a `org.neo4j.ogm.session.SessionFactory` +bean. From eb26492eef5b6b4d6c3f8a5850cc45954712f207 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 2 Aug 2019 17:31:07 +0200 Subject: [PATCH 3/3] Polish --- .../autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java index a70a4df177..93b4703a09 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java @@ -64,7 +64,7 @@ public class Neo4jDataAutoConfiguration { @Bean @ConditionalOnMissingBean(PlatformTransactionManager.class) - public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, Neo4jProperties properties, + public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, ObjectProvider transactionManagerCustomizers) { return customize(new Neo4jTransactionManager(sessionFactory), transactionManagerCustomizers.getIfAvailable()); }