mwielbut的答案部分正确,但还不够 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());
}
}