Skip to content

Commit 15670b8

Browse files
committed
Relocate Neo4J auto-configuration
Move Neo4J auto-configuration from `autoconfigure.neo4j` to `autoconfigure.data.neo4j` since it's intrinsically linked to Spring Data. See spring-projectsgh-5458 See spring-projectsgh-6142
1 parent c22a0e9 commit 15670b8

File tree

11 files changed

+134
-165
lines changed

11 files changed

+134
-165
lines changed

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
2525
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
2728
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
2829
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
29-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
3030
import org.springframework.boot.builder.SpringApplicationBuilder;
3131
import org.springframework.boot.test.util.ApplicationContextTestUtils;
3232
import org.springframework.context.ConfigurableApplicationContext;
@@ -64,7 +64,7 @@ public void testChild() {
6464
@EnableAutoConfiguration(exclude = { ElasticsearchDataAutoConfiguration.class,
6565
ElasticsearchRepositoriesAutoConfiguration.class,
6666
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
67-
Neo4jAutoConfiguration.class, RedisAutoConfiguration.class,
67+
Neo4jDataAutoConfiguration.class, RedisAutoConfiguration.class,
6868
RedisRepositoriesAutoConfiguration.class }, excludeName = {
6969
"org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration" })
7070
public static class Child {
@@ -76,7 +76,7 @@ public static class Child {
7676
ElasticsearchDataAutoConfiguration.class,
7777
ElasticsearchRepositoriesAutoConfiguration.class,
7878
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
79-
Neo4jAutoConfiguration.class, RedisAutoConfiguration.class,
79+
Neo4jDataAutoConfiguration.class, RedisAutoConfiguration.class,
8080
RedisRepositoriesAutoConfiguration.class }, excludeName = {
8181
"org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration" })
8282
public static class Parent {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/neo4j/Neo4jAutoConfiguration.java spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.neo4j;
17+
package org.springframework.boot.autoconfigure.data.neo4j;
1818

1919
import java.util.List;
2020

@@ -57,7 +57,7 @@
5757
@ConditionalOnClass({ Neo4jSession.class, Neo4jOperations.class })
5858
@ConditionalOnMissingBean(Neo4jOperations.class)
5959
@EnableConfigurationProperties(Neo4jProperties.class)
60-
public class Neo4jAutoConfiguration {
60+
public class Neo4jDataAutoConfiguration {
6161

6262
@Configuration
6363
@Import(SessionFactoryProviderConfiguration.class)

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/neo4j/Neo4jProperties.java spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.neo4j;
17+
package org.springframework.boot.autoconfigure.data.neo4j;
1818

1919
import java.net.URI;
2020
import java.net.URISyntaxException;

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jRepositoriesAutoConfiguration.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
2726
import org.springframework.context.annotation.Configuration;
2827
import org.springframework.context.annotation.Import;
2928
import org.springframework.data.neo4j.repository.GraphRepository;
@@ -59,7 +58,7 @@
5958
Neo4jRepositoryConfigurationExtension.class })
6059
@ConditionalOnProperty(prefix = "spring.data.neo4j.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
6160
@Import(Neo4jRepositoriesAutoConfigureRegistrar.class)
62-
@AutoConfigureAfter(Neo4jAutoConfiguration.class)
61+
@AutoConfigureAfter(Neo4jDataAutoConfiguration.class)
6362
public class Neo4jRepositoriesAutoConfiguration {
6463

6564
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositor
3131
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
3232
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
3333
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
34+
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
3435
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
3536
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
3637
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
@@ -70,7 +71,6 @@ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
7071
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
7172
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
7273
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
73-
org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration,\
7474
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
7575
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
7676
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/MixedNeo4jRepositoriesAutoConfigurationTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
3030
import org.springframework.boot.autoconfigure.data.neo4j.country.Country;
3131
import org.springframework.boot.autoconfigure.data.neo4j.country.CountryRepository;
32+
import org.springframework.boot.autoconfigure.data.neo4j.empty.EmptyMarker;
3233
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
33-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
34-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfigurationTests;
3534
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3635
import org.springframework.boot.orm.jpa.EntityScan;
3736
import org.springframework.boot.test.util.EnvironmentTestUtils;
@@ -113,15 +112,15 @@ public void testJpaRepositoryConfigurationWithNeo4jOverlapDisabled()
113112
}
114113

115114
@Configuration
116-
@TestAutoConfigurationPackage(Neo4jAutoConfigurationTests.class)
115+
@TestAutoConfigurationPackage(EmptyMarker.class)
117116
// Not this package or its parent
118117
@EnableNeo4jRepositories(basePackageClasses = Country.class)
119118
protected static class TestConfiguration {
120119

121120
}
122121

123122
@Configuration
124-
@TestAutoConfigurationPackage(Neo4jAutoConfigurationTests.class)
123+
@TestAutoConfigurationPackage(EmptyMarker.class)
125124
@EnableNeo4jRepositories(basePackageClasses = Country.class)
126125
@EntityScan(basePackageClasses = City.class)
127126
@EnableJpaRepositories(basePackageClasses = CityRepository.class)
@@ -130,7 +129,7 @@ protected static class MixedConfiguration {
130129
}
131130

132131
@Configuration
133-
@TestAutoConfigurationPackage(Neo4jAutoConfigurationTests.class)
132+
@TestAutoConfigurationPackage(EmptyMarker.class)
134133
@EntityScan(basePackageClasses = City.class)
135134
@EnableJpaRepositories(basePackageClasses = CityRepository.class)
136135
protected static class JpaConfiguration {
@@ -159,7 +158,8 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
159158
List<String> names = new ArrayList<String>();
160159
for (Class<?> type : new Class<?>[] { DataSourceAutoConfiguration.class,
161160
HibernateJpaAutoConfiguration.class,
162-
JpaRepositoriesAutoConfiguration.class, Neo4jAutoConfiguration.class,
161+
JpaRepositoriesAutoConfiguration.class,
162+
Neo4jDataAutoConfiguration.class,
163163
Neo4jRepositoriesAutoConfiguration.class }) {
164164
names.add(type.getName());
165165
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java

+97-12
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,135 @@
1818

1919
import org.assertj.core.api.Assertions;
2020
import org.junit.After;
21-
import org.junit.Rule;
2221
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
22+
import org.neo4j.ogm.drivers.http.driver.HttpDriver;
23+
import org.neo4j.ogm.session.Session;
24+
import org.neo4j.ogm.session.SessionFactory;
2425

2526
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
27+
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
2628
import org.springframework.boot.autoconfigure.data.neo4j.city.City;
27-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
29+
import org.springframework.boot.test.util.EnvironmentTestUtils;
2830
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
2933
import org.springframework.data.neo4j.mapping.Neo4jMappingContext;
34+
import org.springframework.data.neo4j.template.Neo4jOperations;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.mockito.Mockito.mock;
3038

3139
/**
32-
* Tests for {@link Neo4jAutoConfiguration}.
40+
* Tests for {@link Neo4jDataAutoConfiguration}. Tests can't use the embedded driver as we
41+
* use lucene 4 and Neo4j still requires 3.
3342
*
34-
* @author Josh Long
35-
* @author Oliver Gierke
43+
* @author Stephane Nicoll
44+
* @author Michael Hunger
3645
* @author Vince Bickers
3746
*/
3847
public class Neo4jDataAutoConfigurationTests {
3948

40-
@Rule
41-
public final ExpectedException thrown = ExpectedException.none();
42-
43-
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
49+
private AnnotationConfigApplicationContext context;
4450

4551
@After
4652
public void close() {
47-
this.context.close();
53+
if (this.context != null) {
54+
this.context.close();
55+
}
56+
}
57+
58+
@Test
59+
public void defaultConfiguration() {
60+
load(null, "spring.data.neo4j.uri=http://localhost:8989");
61+
assertThat(this.context.getBeansOfType(Neo4jOperations.class)).hasSize(1);
62+
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class))
63+
.hasSize(1);
64+
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(1);
65+
assertThat(this.context.getBeanDefinition("scopedTarget.getSession").getScope())
66+
.isEqualTo("singleton");
67+
}
68+
69+
@Test
70+
public void customScope() {
71+
load(null, "spring.data.neo4j.uri=http://localhost:8989",
72+
"spring.data.neo4j.session.scope=prototype");
73+
assertThat(this.context.getBeanDefinition("scopedTarget.getSession").getScope())
74+
.isEqualTo("prototype");
75+
}
76+
77+
@Test
78+
public void customNeo4jOperations() {
79+
load(CustomNeo4jOperations.class);
80+
assertThat(this.context.getBean(Neo4jOperations.class))
81+
.isSameAs(this.context.getBean("myNeo4jOperations"));
82+
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class))
83+
.hasSize(0);
84+
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(0);
85+
assertThat(this.context.getBeansOfType(Session.class)).hasSize(0);
86+
}
87+
88+
@Test
89+
public void customConfiguration() {
90+
load(CustomConfiguration.class);
91+
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class))
92+
.isSameAs(this.context.getBean("myConfiguration"));
93+
assertThat(this.context.getBeansOfType(Neo4jOperations.class)).hasSize(1);
94+
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class))
95+
.hasSize(1);
96+
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(1);
4897
}
4998

5099
@Test
51100
public void usesAutoConfigurationPackageToPickUpDomainTypes() {
101+
this.context = new AnnotationConfigApplicationContext();
52102
String cityPackage = City.class.getPackage().getName();
53103
AutoConfigurationPackages.register(this.context, cityPackage);
54-
this.context.register(Neo4jAutoConfiguration.class);
104+
this.context.register(Neo4jDataAutoConfiguration.class);
55105
this.context.refresh();
56106
assertDomainTypesDiscovered(this.context.getBean(Neo4jMappingContext.class),
57107
City.class);
58108
}
59109

110+
public void load(Class<?> config, String... environment) {
111+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
112+
EnvironmentTestUtils.addEnvironment(ctx, environment);
113+
if (config != null) {
114+
ctx.register(config);
115+
}
116+
ctx.register(PropertyPlaceholderAutoConfiguration.class,
117+
Neo4jDataAutoConfiguration.class);
118+
ctx.refresh();
119+
this.context = ctx;
120+
}
121+
60122
private static void assertDomainTypesDiscovered(Neo4jMappingContext mappingContext,
61123
Class<?>... types) {
62124
for (Class<?> type : types) {
63125
Assertions.assertThat(mappingContext.getPersistentEntity(type)).isNotNull();
64126
}
65127
}
66128

129+
@Configuration
130+
static class CustomNeo4jOperations {
131+
132+
@Bean
133+
public Neo4jOperations myNeo4jOperations() {
134+
return mock(Neo4jOperations.class);
135+
}
136+
137+
}
138+
139+
@Configuration
140+
static class CustomConfiguration {
141+
142+
@Bean
143+
public org.neo4j.ogm.config.Configuration myConfiguration() {
144+
org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
145+
configuration.driverConfiguration()
146+
.setDriverClassName(HttpDriver.class.getName());
147+
return configuration;
148+
}
149+
150+
}
151+
67152
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/neo4j/Neo4jPropertiesTests.java spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.neo4j;
17+
package org.springframework.boot.autoconfigure.data.neo4j;
1818

1919
import java.net.URL;
2020
import java.net.URLClassLoader;

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jRepositoriesAutoConfigurationTests.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
2828
import org.springframework.boot.autoconfigure.data.neo4j.city.City;
2929
import org.springframework.boot.autoconfigure.data.neo4j.city.CityRepository;
30-
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
3130
import org.springframework.boot.test.util.EnvironmentTestUtils;
3231
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3332
import org.springframework.context.annotation.Configuration;
@@ -57,32 +56,27 @@ public void close() {
5756
@Test
5857
public void testDefaultRepositoryConfiguration() throws Exception {
5958
prepareApplicationContext(TestConfiguration.class);
60-
6159
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
6260
Neo4jMappingContext mappingContext = this.context
6361
.getBean(Neo4jMappingContext.class);
6462
assertThat(mappingContext.getPersistentEntity(City.class)).isNotNull();
65-
6663
}
6764

6865
@Test
6966
public void testNoRepositoryConfiguration() throws Exception {
7067
prepareApplicationContext(EmptyConfiguration.class);
71-
7268
assertThat(this.context.getBean(SessionFactory.class)).isNotNull();
7369
}
7470

7571
@Test
7672
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
7773
prepareApplicationContext(CustomizedConfiguration.class);
78-
7974
assertThat(this.context.getBean(CityNeo4jRepository.class)).isNotNull();
8075
}
8176

8277
@Test(expected = NoSuchBeanDefinitionException.class)
8378
public void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories() {
8479
prepareApplicationContext(SortOfInvalidCustomConfiguration.class);
85-
8680
this.context.getBean(CityRepository.class);
8781
}
8882

@@ -91,7 +85,7 @@ private void prepareApplicationContext(Class<?>... configurationClasses) {
9185
EnvironmentTestUtils.addEnvironment(this.context,
9286
"spring.data.neo4j.uri=http://localhost:9797");
9387
this.context.register(configurationClasses);
94-
this.context.register(Neo4jAutoConfiguration.class,
88+
this.context.register(Neo4jDataAutoConfiguration.class,
9589
Neo4jRepositoriesAutoConfiguration.class,
9690
PropertyPlaceholderAutoConfiguration.class);
9791
this.context.refresh();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.neo4j.empty;
18+
19+
public class EmptyMarker {
20+
21+
}

0 commit comments

Comments
 (0)