5

neo4jDatabase()は問題environmentありませんが、常に nullgraphDatabaseService()です ... どのように/なぜですか?

@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 の初期化に必要な時点までに、環境メンバー変数が設定されない Autowired 依存関係のスパゲッティにチューニングするだけです。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 に答える