如何将配置传递给模块

How to pass configuration to a module

将一些配置参数传递到我为 neo4j + GraphAware 编写的模块的正确方法是什么?我相信应该有一种方法可以将一些配置条目放入 neo4j.conf 并在我的模块代码中读取它们,但到目前为止我找不到它。

绝对有可能将配置参数传递给您的模块。

最好的方法是查看其他使用此类配置的模块,GraphAware 不怕开源模块 (https://github.com/graphaware?utf8=%E2%9C%93&q=&type=&language=java),您可以找到很多。

我们以uuid-module为例:

在引导程序 class 中,您将找到从配置文件读取配置参数的逻辑:

String uuidProperty = config.get(UUID_PROPERTY);
        if (StringUtils.isNotBlank(uuidProperty)) {
            configuration = configuration.withUuidProperty(uuidProperty);
            LOG.info("uuidProperty set to %s", configuration.getUuidProperty());
        }

https://github.com/graphaware/neo4j-uuid/blob/master/src/main/java/com/graphaware/module/uuid/UuidBootstrapper.java#L55

找到的参数用于创建不可变配置class:

https://github.com/graphaware/neo4j-uuid/blob/master/src/main/java/com/graphaware/module/uuid/UuidConfiguration.java

模块引导结束,然后将配置对象传递给模块的构造函数:

return new UuidModule(moduleId, configuration, database);

https://github.com/graphaware/neo4j-uuid/blob/master/src/main/java/com/graphaware/module/uuid/UuidBootstrapper.java#L89

然后您可以将此模块与配置一起使用:

public UuidModule(String moduleId, UuidConfiguration configuration, GraphDatabaseService database) {
        super(moduleId);        
        this.uuidConfiguration = configuration;
        this.uuidGenerator = instantiateUuidGenerator(configuration, database);
        this.uuidIndexer = new LegacyIndexer(database, configuration);
    }

https://github.com/graphaware/neo4j-uuid/blob/master/src/main/java/com/graphaware/module/uuid/UuidModule.java