Skip to content

Commit 33dda3a

Browse files
committed
Merge pull request #24239 from meistermeier
* pr/24239: Polish "Detect Persistent and RelationshipProperties with Neo4j" Detect Persistent and RelationshipProperties with Neo4j Closes gh-24239
2 parents 1f71927 + 0691ba6 commit 33dda3a

File tree

7 files changed

+162
-1
lines changed

7 files changed

+162
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
import org.springframework.context.ApplicationContext;
3636
import org.springframework.context.annotation.Bean;
3737
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.data.annotation.Persistent;
3839
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
3940
import org.springframework.data.neo4j.core.Neo4jClient;
4041
import org.springframework.data.neo4j.core.Neo4jOperations;
4142
import org.springframework.data.neo4j.core.Neo4jTemplate;
4243
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
4344
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
4445
import org.springframework.data.neo4j.core.schema.Node;
46+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
4547
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
4648
import org.springframework.data.neo4j.repository.config.Neo4jRepositoryConfigurationExtension;
4749
import org.springframework.transaction.PlatformTransactionManager;
@@ -76,7 +78,8 @@ public Neo4jConversions neo4jConversions() {
7678
@ConditionalOnMissingBean
7779
public Neo4jMappingContext neo4jMappingContext(ApplicationContext applicationContext,
7880
Neo4jConversions neo4jConversions) throws ClassNotFoundException {
79-
Set<Class<?>> initialEntityClasses = new EntityScanner(applicationContext).scan(Node.class);
81+
Set<Class<?>> initialEntityClasses = new EntityScanner(applicationContext).scan(Node.class, Persistent.class,
82+
RelationshipProperties.class);
8083
Neo4jMappingContext context = new Neo4jMappingContext(neo4jConversions);
8184
context.setInitialEntitySet(initialEntityClasses);
8285
return context;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
23+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestNode;
24+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestNonAnnotated;
25+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestPersistent;
26+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestRelationshipProperties;
2227
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
2328
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2429
import org.springframework.context.annotation.Bean;
@@ -29,6 +34,7 @@
2934
import org.springframework.data.neo4j.core.Neo4jOperations;
3035
import org.springframework.data.neo4j.core.Neo4jTemplate;
3136
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
37+
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
3238
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
3339
import org.springframework.transaction.PlatformTransactionManager;
3440
import org.springframework.transaction.ReactiveTransactionManager;
@@ -137,6 +143,17 @@ void shouldReuseExistingTransactionManager() {
137143
.hasBean("myCustomTransactionManager"));
138144
}
139145

146+
@Test
147+
void shouldFilterInitialEntityScanWithKnownAnnotations() {
148+
this.contextRunner.withUserConfiguration(EntityScanConfig.class).run((context) -> {
149+
Neo4jMappingContext mappingContext = context.getBean(Neo4jMappingContext.class);
150+
assertThat(mappingContext.hasPersistentEntityFor(TestNode.class)).isTrue();
151+
assertThat(mappingContext.hasPersistentEntityFor(TestPersistent.class)).isTrue();
152+
assertThat(mappingContext.hasPersistentEntityFor(TestRelationshipProperties.class)).isTrue();
153+
assertThat(mappingContext.hasPersistentEntityFor(TestNonAnnotated.class)).isFalse();
154+
});
155+
}
156+
140157
@Configuration(proxyBeanMethods = false)
141158
static class CustomDatabaseSelectionProviderConfiguration {
142159

@@ -147,4 +164,10 @@ DatabaseSelectionProvider databaseSelectionProvider() {
147164

148165
}
149166

167+
@Configuration(proxyBeanMethods = false)
168+
@TestAutoConfigurationPackage(TestPersistent.class)
169+
static class EntityScanConfig {
170+
171+
}
172+
150173
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
import reactor.test.StepVerifier;
2222

2323
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
25+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestNode;
26+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestNonAnnotated;
27+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestPersistent;
28+
import org.springframework.boot.autoconfigure.data.neo4j.scan.TestRelationshipProperties;
2429
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
2530
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2631
import org.springframework.context.annotation.Bean;
@@ -30,6 +35,7 @@
3035
import org.springframework.data.neo4j.core.ReactiveNeo4jClient;
3136
import org.springframework.data.neo4j.core.ReactiveNeo4jOperations;
3237
import org.springframework.data.neo4j.core.ReactiveNeo4jTemplate;
38+
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
3339
import org.springframework.transaction.ReactiveTransactionManager;
3440
import org.springframework.transaction.TransactionManager;
3541

@@ -121,6 +127,17 @@ void shouldUseExistingReactiveTransactionManager() {
121127
.hasSingleBean(TransactionManager.class));
122128
}
123129

130+
@Test
131+
void shouldFilterInitialEntityScanWithKnownAnnotations() {
132+
this.contextRunner.withUserConfiguration(EntityScanConfig.class).run((context) -> {
133+
Neo4jMappingContext mappingContext = context.getBean(Neo4jMappingContext.class);
134+
assertThat(mappingContext.hasPersistentEntityFor(TestNode.class)).isTrue();
135+
assertThat(mappingContext.hasPersistentEntityFor(TestPersistent.class)).isTrue();
136+
assertThat(mappingContext.hasPersistentEntityFor(TestRelationshipProperties.class)).isTrue();
137+
assertThat(mappingContext.hasPersistentEntityFor(TestNonAnnotated.class)).isFalse();
138+
});
139+
}
140+
124141
@Configuration(proxyBeanMethods = false)
125142
static class CustomReactiveDatabaseSelectionProviderConfiguration {
126143

@@ -131,4 +148,10 @@ ReactiveDatabaseSelectionProvider databaseNameProvider() {
131148

132149
}
133150

151+
@Configuration(proxyBeanMethods = false)
152+
@TestAutoConfigurationPackage(TestPersistent.class)
153+
static class EntityScanConfig {
154+
155+
}
156+
134157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.scan;
18+
19+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
20+
import org.springframework.data.neo4j.core.schema.Id;
21+
import org.springframework.data.neo4j.core.schema.Node;
22+
23+
@Node
24+
public class TestNode {
25+
26+
@Id
27+
@GeneratedValue
28+
private Long id;
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.scan;
18+
19+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
20+
import org.springframework.data.neo4j.core.schema.Id;
21+
22+
public class TestNonAnnotated {
23+
24+
@Id
25+
@GeneratedValue
26+
private Long id;
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.scan;
18+
19+
import org.springframework.data.annotation.Persistent;
20+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
21+
import org.springframework.data.neo4j.core.schema.Id;
22+
23+
@Persistent
24+
public class TestPersistent {
25+
26+
@Id
27+
@GeneratedValue
28+
private Long id;
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.scan;
18+
19+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
20+
21+
@RelationshipProperties
22+
public class TestRelationshipProperties {
23+
24+
}

0 commit comments

Comments
 (0)