|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.neo4j;
|
18 | 18 |
|
19 |
| -import java.util.List; |
| 19 | +import java.util.Set; |
20 | 20 |
|
21 |
| -import org.neo4j.ogm.session.SessionFactory; |
22 |
| -import org.neo4j.ogm.session.event.EventListener; |
| 21 | +import org.neo4j.driver.Driver; |
23 | 22 |
|
24 |
| -import org.springframework.beans.factory.BeanFactory; |
25 |
| -import org.springframework.beans.factory.ObjectProvider; |
26 |
| -import org.springframework.boot.autoconfigure.AutoConfigurationPackages; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
27 | 24 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
28 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
29 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
30 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
31 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
32 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; |
33 |
| -import org.springframework.boot.autoconfigure.domain.EntityScanPackages; |
34 |
| -import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; |
| 27 | +import org.springframework.boot.autoconfigure.domain.EntityScanner; |
| 28 | +import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration; |
35 | 29 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
| 30 | +import org.springframework.context.ApplicationContext; |
36 | 31 | import org.springframework.context.annotation.Bean;
|
37 | 32 | import org.springframework.context.annotation.Configuration;
|
38 | 33 | import org.springframework.context.annotation.Import;
|
39 |
| -import org.springframework.data.neo4j.transaction.Neo4jTransactionManager; |
40 |
| -import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor; |
41 |
| -import org.springframework.transaction.PlatformTransactionManager; |
42 |
| -import org.springframework.util.StringUtils; |
43 |
| -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
44 |
| -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 34 | +import org.springframework.data.neo4j.core.convert.Neo4jConversions; |
| 35 | +import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext; |
| 36 | +import org.springframework.data.neo4j.core.schema.Node; |
45 | 37 |
|
46 | 38 | /**
|
47 |
| - * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Neo4j. |
| 39 | + * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Neo4j. Automatic |
| 40 | + * configuration of base infrastructure that imports configuration for both imperative and |
| 41 | + * reactive Neo4j repositories. Depends on the configured Neo4j driver. |
48 | 42 | *
|
49 | 43 | * @author Michael Hunger
|
50 | 44 | * @author Josh Long
|
51 | 45 | * @author Vince Bickers
|
52 | 46 | * @author Stephane Nicoll
|
53 | 47 | * @author Kazuki Shimizu
|
54 |
| - * @author Michael Simons |
| 48 | + * @author Michael J Simons |
55 | 49 | * @since 1.4.0
|
56 | 50 | */
|
57 | 51 | @Configuration(proxyBeanMethods = false)
|
58 |
| -@ConditionalOnClass({ SessionFactory.class, Neo4jTransactionManager.class, PlatformTransactionManager.class }) |
59 |
| -@EnableConfigurationProperties(Neo4jProperties.class) |
60 |
| -@Import(Neo4jBookmarkManagementConfiguration.class) |
| 52 | +@ConditionalOnBean(Driver.class) |
| 53 | +@EnableConfigurationProperties(Neo4jDataProperties.class) |
| 54 | +@AutoConfigureBefore(TransactionAutoConfiguration.class) |
| 55 | +@Import({ Neo4jImperativeDataConfiguration.class, Neo4jReactiveDataConfiguration.class }) |
61 | 56 | public class Neo4jDataAutoConfiguration {
|
62 | 57 |
|
63 | 58 | @Bean
|
64 |
| - @ConditionalOnMissingBean(PlatformTransactionManager.class) |
65 |
| - public Neo4jTransactionManager transactionManager(SessionFactory sessionFactory, |
66 |
| - ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) { |
67 |
| - Neo4jTransactionManager transactionManager = new Neo4jTransactionManager(sessionFactory); |
68 |
| - transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager)); |
69 |
| - return transactionManager; |
| 59 | + @ConditionalOnMissingBean |
| 60 | + public Neo4jConversions neo4jConversions() { |
| 61 | + return new Neo4jConversions(); |
70 | 62 | }
|
71 | 63 |
|
72 |
| - @Configuration(proxyBeanMethods = false) |
73 |
| - @ConditionalOnMissingBean(SessionFactory.class) |
74 |
| - static class Neo4jOgmSessionFactoryConfiguration { |
75 |
| - |
76 |
| - @Bean |
77 |
| - @ConditionalOnMissingBean |
78 |
| - org.neo4j.ogm.config.Configuration configuration(Neo4jProperties properties) { |
79 |
| - return properties.createConfiguration(); |
80 |
| - } |
81 |
| - |
82 |
| - @Bean |
83 |
| - SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration, BeanFactory beanFactory, |
84 |
| - ObjectProvider<EventListener> eventListeners) { |
85 |
| - SessionFactory sessionFactory = new SessionFactory(configuration, getPackagesToScan(beanFactory)); |
86 |
| - eventListeners.orderedStream().forEach(sessionFactory::register); |
87 |
| - return sessionFactory; |
88 |
| - } |
89 |
| - |
90 |
| - private String[] getPackagesToScan(BeanFactory beanFactory) { |
91 |
| - List<String> packages = EntityScanPackages.get(beanFactory).getPackageNames(); |
92 |
| - if (packages.isEmpty() && AutoConfigurationPackages.has(beanFactory)) { |
93 |
| - packages = AutoConfigurationPackages.get(beanFactory); |
94 |
| - } |
95 |
| - return StringUtils.toStringArray(packages); |
96 |
| - } |
97 |
| - |
98 |
| - } |
99 |
| - |
100 |
| - @Configuration(proxyBeanMethods = false) |
101 |
| - @ConditionalOnWebApplication(type = Type.SERVLET) |
102 |
| - @ConditionalOnClass({ WebMvcConfigurer.class, OpenSessionInViewInterceptor.class }) |
103 |
| - @ConditionalOnMissingBean(OpenSessionInViewInterceptor.class) |
104 |
| - @ConditionalOnProperty(prefix = "spring.data.neo4j", name = "open-in-view", havingValue = "true") |
105 |
| - static class Neo4jWebConfiguration { |
106 |
| - |
107 |
| - @Bean |
108 |
| - OpenSessionInViewInterceptor neo4jOpenSessionInViewInterceptor() { |
109 |
| - return new OpenSessionInViewInterceptor(); |
110 |
| - } |
111 |
| - |
112 |
| - @Bean |
113 |
| - WebMvcConfigurer neo4jOpenSessionInViewInterceptorConfigurer(OpenSessionInViewInterceptor interceptor) { |
114 |
| - return new WebMvcConfigurer() { |
115 |
| - |
116 |
| - @Override |
117 |
| - public void addInterceptors(InterceptorRegistry registry) { |
118 |
| - registry.addWebRequestInterceptor(interceptor); |
119 |
| - } |
| 64 | + @Bean |
| 65 | + @ConditionalOnMissingBean |
| 66 | + public Neo4jMappingContext neo4jMappingContext(ApplicationContext applicationContext, |
| 67 | + Neo4jConversions neo4jConversions) throws ClassNotFoundException { |
120 | 68 |
|
121 |
| - }; |
122 |
| - } |
| 69 | + Set<Class<?>> initialEntityClasses = new EntityScanner(applicationContext).scan(Node.class); |
| 70 | + Neo4jMappingContext context = new Neo4jMappingContext(neo4jConversions); |
| 71 | + context.setInitialEntitySet(initialEntityClasses); |
123 | 72 |
|
| 73 | + return context; |
124 | 74 | }
|
125 | 75 |
|
126 | 76 | }
|
0 commit comments