5

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。

4

2 回答 2

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。它只是调整成自动装配依赖项的意大利面条,其中环境成员变量在初始化 graphDatabaseService 所需的时间从未设置。您可以使用 a 使其工作,@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
}
于 2013-06-05T19:21:48.107 回答
0

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());
    }
}
于 2015-06-12T13:18:54.977 回答