neo4jDatabase()
没问题,但environment
graphDatabaseService()
总是空的...怎么/为什么?
@Configuration
@PropertySource("classpath:/neo4j.properties")
@EnableNeo4jRepositories("reservation.repository.neo4j")
public class Neo4jConfig extends Neo4jConfiguration {
@Inject
Environment environment;
@Bean(initMethod = "setupDb")
public Neo4jDatabase neo4jDatabase() {
// Environment fine here...
return new Neo4jDatabase(this.environment.getProperty("data.file.path"));
}
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
if (environment == null) {
// Always gets here for some reason...why?
return new EmbeddedGraphDatabase("/Temp/neo4j/database");
} else {
return new EmbeddedGraphDatabase(this.environment.getProperty("database.path"));
}
}
}
版本: Spring 3.2.0.RELEASE,spring-data-neo4j 2.1.0.RELEASE。
答案 0 :(得分:1)
如果其他人遇到同样的问题 - 以下内容对我有用:
@Configuration
@PropertySource("classpath:neo4j.properties")
@EnableNeo4jRepositories(basePackages = "com.mydomain.neo4j.repo")
public class Neo4jConfig
{
@Autowired
Environment environment;
@Bean(name="graphDatabaseService", destroyMethod = "shutdown")
public GraphDatabaseService getGraphDatabaseService()
{
// any custom graph db initialization
return new EmbeddedGraphDatabase(this.environment.getProperty("database.path"));
}
}
注意:我没有扩展Neo4jConfiguration。它只是调整了Autowired依赖项的意大利面,其中Environment成员变量从未在graphDatabaseService初始化所需的时间设置。你可以使用@PostConstruct
让它工作,但你最终得到了一堆NotInTransactionException
。我没有时间深入研究原因 - 相反,在您的主AppConfig类中,您只需将自定义Neo4j配置导入为基本抽象Neo4j配置类。从本质上讲,您正在使用XML配置执行的代码。
@Configuration
@Import({Neo4jConfig.class, Neo4jConfiguration.class})
@ComponentScan(basePackages = {"com.mydomain"}, excludeFilters = @Filter({Controller.class, Configuration.class}))
public class MainConfig
{
// any other configuration you have
}
答案 1 :(得分:0)
来自 mwiel但的答案部分正确,但它并非100%足够,因为人们也可以为域类指定基础包。
检查以下源代码以获得完整的工作实现
属性文件加载
package com.example.analytics.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@ConfigurationProperties(locations = "classpath:application.yml", ignoreUnknownFields = false, prefix = "neo4j")
public class DatabaseConnectionProperties {
private static final Logger LOG = LoggerFactory.getLogger(DatabaseConnectionProperties.class);
private String uri;
private String username;
private String password;
public String getUri() {
return uri;
}
public void setUri(final String uri) {
this.uri = uri;
}
public String getUsername() {
return username;
}
public void setUsername(final String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
@PostConstruct
public void uponConstruction(){
LOG.debug("Construction of the class {} has been completed!", this.getClass().getName());
LOG.debug("Database connection properties have been loaded from application.yml in the classpath");
LOG.debug("Initialised URI: {} , username: {}, password: {}", uri, username, password);
}
}
数据库连接配置
package com.example.analytics.config;
import org.neo4j.graphdb.GraphDatabaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
@Configuration
@ComponentScan(basePackages = "com.example.analytics.config")
@EnableConfigurationProperties(DatabaseConnectionProperties.class)
public class DatabaseConnectionConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(DatabaseConnectionConfiguration.class);
@Autowired
private DatabaseConnectionProperties databaseConnectionProperties;
/**
* Connect to the remote instance of Neo4j through its REST interface
* @return
*/
@Bean(name="graphDatabaseService", destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
LOG.debug("Creating the graph database service with credentials");
final String uri = databaseConnectionProperties.getUri();
final String username = databaseConnectionProperties.getUsername();
final String password = databaseConnectionProperties.getPassword();
LOG.debug("URI: {}", uri);
LOG.debug("Username: {}", username);
LOG.debug("Password: {}", password);
final SpringCypherRestGraphDatabase graphDatabase = new SpringCypherRestGraphDatabase(uri, username, password);
LOG.debug("Graph database instance has been successfully created!");
return graphDatabase;
}
}
主要配置
package com.example.analytics.config;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.analytics.repository")
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@Import({DatabaseConnectionConfiguration.class})
public class MainConfiguration extends Neo4jConfiguration {
public MainConfiguration() {
setBasePackage("com.example.analytics.model");
}
}
主要申请
package com.example.analytics;
import com.example.analytics.config.DatabaseConnectionConfiguration;
import com.example.analytics.config.MainConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import javax.annotation.PostConstruct;
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.example.analytics"})
@Import(MainConfiguration.class)
public class AnalyticsApplication {
private static final Logger LOG = LoggerFactory.getLogger(DatabaseConnectionConfiguration.class);
public static void main(final String[] args) {
SpringApplication.run(AnalyticsApplication.class, args);
}
@PostConstruct
public void uponConstruction() {
LOG.debug("Construction of the class {} has been completed!", this.getClass().getName());
}
}