Merge pull request #17662 from michael-simons

* pr/17662:
  Polish "Refine back-off strategy of Neo4j SessionFactory"
  Refine back-off strategy of Neo4j SessionFactory

Closes gh-17662
pull/17807/head
Stephane Nicoll 5 years ago
commit dd5ec49232

@ -53,29 +53,15 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* @author Vince Bickers * @author Vince Bickers
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Kazuki Shimizu * @author Kazuki Shimizu
* @author Michael Simons
* @since 1.4.0 * @since 1.4.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ SessionFactory.class, Neo4jTransactionManager.class, PlatformTransactionManager.class }) @ConditionalOnClass({ SessionFactory.class, Neo4jTransactionManager.class, PlatformTransactionManager.class })
@ConditionalOnMissingBean(SessionFactory.class)
@EnableConfigurationProperties(Neo4jProperties.class) @EnableConfigurationProperties(Neo4jProperties.class)
@Import(Neo4jBookmarkManagementConfiguration.class) @Import(Neo4jBookmarkManagementConfiguration.class)
public class Neo4jDataAutoConfiguration { 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<EventListener> eventListeners) {
SessionFactory sessionFactory = new SessionFactory(configuration, getPackagesToScan(applicationContext));
eventListeners.stream().forEach(sessionFactory::register);
return sessionFactory;
}
@Bean @Bean
@ConditionalOnMissingBean(PlatformTransactionManager.class) @ConditionalOnMissingBean(PlatformTransactionManager.class)
public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, Neo4jProperties properties, public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, Neo4jProperties properties,
@ -91,6 +77,24 @@ public class Neo4jDataAutoConfiguration {
return transactionManager; return transactionManager;
} }
@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<EventListener> eventListeners) {
SessionFactory sessionFactory = new SessionFactory(configuration, getPackagesToScan(applicationContext));
eventListeners.stream().forEach(sessionFactory::register);
return sessionFactory;
}
private String[] getPackagesToScan(ApplicationContext applicationContext) { private String[] getPackagesToScan(ApplicationContext applicationContext) {
List<String> packages = EntityScanPackages.get(applicationContext).getPackageNames(); List<String> packages = EntityScanPackages.get(applicationContext).getPackageNames();
if (packages.isEmpty() && AutoConfigurationPackages.has(applicationContext)) { if (packages.isEmpty() && AutoConfigurationPackages.has(applicationContext)) {
@ -99,6 +103,8 @@ public class Neo4jDataAutoConfiguration {
return StringUtils.toStringArray(packages); return StringUtils.toStringArray(packages);
} }
}
@Configuration @Configuration
@ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ WebMvcConfigurer.class, OpenSessionInViewInterceptor.class }) @ConditionalOnClass({ WebMvcConfigurer.class, OpenSessionInViewInterceptor.class })

@ -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 @Test
public void customConfiguration() { public void customConfiguration() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {

@ -4385,9 +4385,9 @@ properties, as shown in the following example:
spring.data.neo4j.password=secret spring.data.neo4j.password=secret
---- ----
You can take full control over the session creation by adding a You can take full control over the session creation by either adding a
`org.neo4j.ogm.config.Configuration` `@Bean`. Also, adding a `@Bean` of type `org.neo4j.ogm.config.Configuration` bean or a `org.neo4j.ogm.session.SessionFactory`
`SessionFactory` disables the auto-configuration and gives you full control. bean.

Loading…
Cancel
Save