Register any EventListener beans with auto-configured SessionFactory
Closes gh-7531
This commit is contained in:
parent
0db924c369
commit
149f19cd58
|
|
@ -19,7 +19,9 @@ package org.springframework.boot.autoconfigure.data.neo4j;
|
|||
import java.util.List;
|
||||
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.neo4j.ogm.session.event.EventListener;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
|
|
@ -63,8 +65,18 @@ public class Neo4jDataAutoConfiguration {
|
|||
|
||||
@Bean
|
||||
public SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration,
|
||||
ApplicationContext applicationContext) {
|
||||
return new SessionFactory(configuration, getPackagesToScan(applicationContext));
|
||||
ApplicationContext applicationContext,
|
||||
ObjectProvider<List<EventListener>> eventListenersProvider) {
|
||||
SessionFactory sessionFactory = new SessionFactory(configuration,
|
||||
getPackagesToScan(applicationContext));
|
||||
List<EventListener> providedEventListeners = eventListenersProvider
|
||||
.getIfAvailable();
|
||||
if (providedEventListeners != null) {
|
||||
for (EventListener eventListener : providedEventListeners) {
|
||||
sessionFactory.register(eventListener);
|
||||
}
|
||||
}
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ import org.assertj.core.api.Assertions;
|
|||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.neo4j.ogm.drivers.http.driver.HttpDriver;
|
||||
import org.neo4j.ogm.session.Session;
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.neo4j.ogm.session.event.Event;
|
||||
import org.neo4j.ogm.session.event.EventListener;
|
||||
import org.neo4j.ogm.session.event.PersistenceEvent;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
|
|
@ -37,7 +41,9 @@ import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor;
|
|||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link Neo4jDataAutoConfiguration}. Tests can't use the embedded driver as we
|
||||
|
|
@ -117,6 +123,17 @@ public class Neo4jDataAutoConfigurationTests {
|
|||
.hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eventListenersAreAutoRegistered() {
|
||||
load(EventListenerConfiguration.class);
|
||||
Session session = this.context.getBean(SessionFactory.class).openSession();
|
||||
session.notifyListeners(new PersistenceEvent(null, Event.TYPE.PRE_SAVE));
|
||||
verify(this.context.getBean("eventListenerOne", EventListener.class))
|
||||
.onPreSave(any(Event.class));
|
||||
verify(this.context.getBean("eventListenerTwo", EventListener.class))
|
||||
.onPreSave(any(Event.class));
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
|
|
@ -169,4 +186,19 @@ public class Neo4jDataAutoConfigurationTests {
|
|||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class EventListenerConfiguration {
|
||||
|
||||
@Bean
|
||||
public EventListener eventListenerOne() {
|
||||
return mock(EventListener.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EventListener eventListenerTwo() {
|
||||
return mock(EventListener.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue