Skip to content

Commit aa171d1

Browse files
committed
Merge pull request spring-projects#5458 from jexp/contrib
* pr/5458: Polish contribution Add Neo4j support
2 parents a413ace + fd43779 commit aa171d1

52 files changed

Lines changed: 2700 additions & 157 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

spring-boot-actuator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@
166166
<artifactId>spring-data-mongodb</artifactId>
167167
<optional>true</optional>
168168
</dependency>
169+
<dependency>
170+
<groupId>org.springframework.data</groupId>
171+
<artifactId>spring-data-neo4j</artifactId>
172+
<optional>true</optional>
173+
</dependency>
169174
<dependency>
170175
<groupId>org.springframework.data</groupId>
171176
<artifactId>spring-data-redis</artifactId>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
2727
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
2828
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
2930
import org.springframework.boot.builder.SpringApplicationBuilder;
3031
import org.springframework.boot.test.util.ApplicationContextTestUtils;
3132
import org.springframework.context.ConfigurableApplicationContext;
@@ -63,6 +64,7 @@ public void testChild() {
6364
@EnableAutoConfiguration(exclude = { ElasticsearchDataAutoConfiguration.class,
6465
ElasticsearchRepositoriesAutoConfiguration.class,
6566
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
67+
Neo4jAutoConfiguration.class,
6668
RedisAutoConfiguration.class,
6769
RedisRepositoriesAutoConfiguration.class }, excludeName = {
6870
"org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration" })
@@ -75,6 +77,7 @@ public static class Child {
7577
ElasticsearchDataAutoConfiguration.class,
7678
ElasticsearchRepositoriesAutoConfiguration.class,
7779
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
80+
Neo4jAutoConfiguration.class,
7881
RedisAutoConfiguration.class,
7982
RedisRepositoriesAutoConfiguration.class }, excludeName = {
8083
"org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration" })

spring-boot-autoconfigure/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@
379379
</exclusion>
380380
</exclusions>
381381
</dependency>
382+
<dependency>
383+
<groupId>org.springframework.data</groupId>
384+
<artifactId>spring-data-neo4j</artifactId>
385+
<optional>true</optional>
386+
<exclusions>
387+
<exclusion>
388+
<artifactId>jcl-over-slf4j</artifactId>
389+
<groupId>org.slf4j</groupId>
390+
</exclusion>
391+
</exclusions>
392+
</dependency>
382393
<dependency>
383394
<groupId>org.springframework.data</groupId>
384395
<artifactId>spring-data-redis</artifactId>
@@ -635,5 +646,6 @@
635646
<artifactId>tomcat-embed-jasper</artifactId>
636647
<scope>test</scope>
637648
</dependency>
649+
638650
</dependencies>
639651
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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;
18+
19+
import org.neo4j.ogm.session.Neo4jSession;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26+
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
29+
import org.springframework.data.neo4j.repository.GraphRepository;
30+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
31+
import org.springframework.data.neo4j.repository.config.Neo4jRepositoryConfigurationExtension;
32+
import org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Neo4j
36+
* Repositories.
37+
* <p>
38+
* Activates when there is no bean of type
39+
* {@link org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean}
40+
* configured in the context, the Spring Data Neo4j
41+
* {@link org.springframework.data.neo4j.repository.GraphRepository} type is on the
42+
* classpath, the Neo4j client driver API is on the classpath, and there is no other
43+
* configured {@link org.springframework.data.neo4j.repository.GraphRepository}.
44+
* <p>
45+
* Once in effect, the auto-configuration is the equivalent of enabling Neo4j repositories
46+
* using the
47+
* {@link org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories}
48+
* annotation.
49+
*
50+
* @author Dave Syer
51+
* @author Oliver Gierke
52+
* @author Josh Long
53+
* @since 1.4.0
54+
* @see EnableNeo4jRepositories
55+
*/
56+
@Configuration
57+
@ConditionalOnClass({ Neo4jSession.class, GraphRepository.class })
58+
@ConditionalOnMissingBean({ GraphRepositoryFactoryBean.class, Neo4jRepositoryConfigurationExtension.class })
59+
@ConditionalOnProperty(prefix = "spring.data.neo4j.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
60+
@Import(Neo4jRepositoriesAutoConfigureRegistrar.class)
61+
@AutoConfigureAfter(Neo4jAutoConfiguration.class)
62+
public class Neo4jRepositoriesAutoConfiguration {
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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;
18+
19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport;
22+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
23+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
24+
import org.springframework.data.neo4j.repository.config.Neo4jRepositoryConfigurationExtension;
25+
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
26+
27+
/**
28+
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Neo4j
29+
* Repositories.
30+
*
31+
* @author Michael Hunger
32+
*/
33+
class Neo4jRepositoriesAutoConfigureRegistrar extends
34+
AbstractRepositoryConfigurationSourceSupport {
35+
36+
@Override
37+
protected Class<? extends Annotation> getAnnotation() {
38+
return EnableNeo4jRepositories.class;
39+
}
40+
41+
@Override
42+
protected Class<?> getConfiguration() {
43+
return EnableNeo4jRepositoriesConfiguration.class;
44+
}
45+
46+
@Override
47+
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
48+
return new Neo4jRepositoryConfigurationExtension();
49+
}
50+
51+
@EnableNeo4jRepositories
52+
private static class EnableNeo4jRepositoriesConfiguration {
53+
}
54+
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2012-2015 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+
/**
18+
* Auto-configuration for Spring Data Neo4j.
19+
*/
20+
package org.springframework.boot.autoconfigure.data.neo4j;
21+
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.neo4j;
18+
19+
import java.util.List;
20+
21+
import org.neo4j.ogm.session.Neo4jSession;
22+
import org.neo4j.ogm.session.Session;
23+
import org.neo4j.ogm.session.SessionFactory;
24+
25+
import org.springframework.beans.BeansException;
26+
import org.springframework.beans.factory.BeanFactory;
27+
import org.springframework.beans.factory.BeanFactoryAware;
28+
import org.springframework.beans.factory.ObjectProvider;
29+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30+
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
31+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
35+
import org.springframework.boot.neo4j.SessionFactoryProvider;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.context.annotation.Import;
39+
import org.springframework.context.annotation.Scope;
40+
import org.springframework.context.annotation.ScopedProxyMode;
41+
import org.springframework.data.neo4j.config.Neo4jConfiguration;
42+
import org.springframework.data.neo4j.template.Neo4jOperations;
43+
import org.springframework.data.neo4j.template.Neo4jTemplate;
44+
45+
/**
46+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Neo4j support.
47+
* <p>
48+
* Registers a {@link Neo4jTemplate} bean if no other bean of
49+
* the same type is configured.
50+
*
51+
* @author Michael Hunger
52+
* @author Josh Long
53+
* @author Vince Bickers
54+
* @author Stephane Nicoll
55+
* @since 1.4.0
56+
*/
57+
@Configuration
58+
@ConditionalOnClass({Neo4jSession.class, Neo4jOperations.class})
59+
@ConditionalOnMissingBean(Neo4jOperations.class)
60+
@EnableConfigurationProperties(Neo4jProperties.class)
61+
public class Neo4jAutoConfiguration {
62+
63+
@Configuration
64+
@Import(SessionFactoryProviderConfiguration.class)
65+
public static class SpringBootNeo4jConfiguration extends Neo4jConfiguration {
66+
67+
private final ObjectProvider<SessionFactoryProvider> sessionFactoryProvider;
68+
69+
public SpringBootNeo4jConfiguration(ObjectProvider<SessionFactoryProvider> sessionFactoryProvider) {
70+
this.sessionFactoryProvider = sessionFactoryProvider;
71+
}
72+
73+
@Override
74+
public SessionFactory getSessionFactory() {
75+
SessionFactoryProvider provider = this.sessionFactoryProvider.getObject();
76+
return provider.getSessionFactory();
77+
}
78+
79+
@Bean
80+
@Scope(scopeName = "${spring.data.neo4j.session.scope:singleton}",
81+
proxyMode = ScopedProxyMode.TARGET_CLASS)
82+
@Override
83+
public Session getSession() throws Exception {
84+
return getSessionFactory().openSession();
85+
}
86+
87+
}
88+
89+
@Configuration
90+
@Import(Neo4jConfigurationConfiguration.class)
91+
static class SessionFactoryProviderConfiguration implements BeanFactoryAware {
92+
93+
private final org.neo4j.ogm.config.Configuration configuration;
94+
95+
private ConfigurableListableBeanFactory beanFactory;
96+
97+
SessionFactoryProviderConfiguration(org.neo4j.ogm.config.Configuration configuration) {
98+
this.configuration = configuration;
99+
}
100+
101+
@Bean
102+
@ConditionalOnMissingBean
103+
public SessionFactoryProvider sessionFactoryProvider() {
104+
SessionFactoryProvider provider = new SessionFactoryProvider();
105+
provider.setConfiguration(this.configuration);
106+
provider.setPackagesToScan(getPackagesToScan());
107+
return provider;
108+
}
109+
110+
@Override
111+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
112+
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
113+
}
114+
115+
protected String[] getPackagesToScan() {
116+
if (AutoConfigurationPackages.has(this.beanFactory)) {
117+
List<String> basePackages = AutoConfigurationPackages.get(this.beanFactory);
118+
return basePackages.toArray(new String[basePackages.size()]);
119+
}
120+
return new String[0];
121+
}
122+
123+
}
124+
125+
@Configuration
126+
static class Neo4jConfigurationConfiguration {
127+
128+
private final Neo4jProperties properties;
129+
130+
Neo4jConfigurationConfiguration(Neo4jProperties properties) {
131+
this.properties = properties;
132+
}
133+
134+
@Bean
135+
@ConditionalOnMissingBean
136+
public org.neo4j.ogm.config.Configuration configuration() {
137+
return this.properties.createConfiguration();
138+
}
139+
140+
}
141+
142+
}

0 commit comments

Comments
 (0)