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 c892385ca3..11e9e827ab 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,32 +53,18 @@ 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(proxyBeanMethods = false) @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, + public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, ObjectProvider transactionManagerCustomizers) { return customize(new Neo4jTransactionManager(sessionFactory), transactionManagerCustomizers.getIfAvailable()); } @@ -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(proxyBeanMethods = false) + @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(proxyBeanMethods = false) 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 8643fb6a97..8c63012c34 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 @@ -99,6 +99,16 @@ class Neo4jDataAutoConfigurationTests { }); } + @Test + void customSessionFactoryShouldNotDisableOtherDefaults() { + this.contextRunner.withUserConfiguration(CustomSessionFactory.class).run((context) -> { + assertThat(context).hasSingleBean(SessionFactory.class); + assertThat(context.getBean(SessionFactory.class)).isSameAs(context.getBean("customSessionFactory")); + assertThat(context).hasSingleBean(Neo4jTransactionManager.class); + assertThat(context).hasSingleBean(OpenSessionInViewInterceptor.class); + }); + } + @Test void customConfiguration() { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { 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 88252ac9e9..25f54e9886 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 @@ -4667,9 +4667,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.