java 未从www.example.com获取值application.properties

cuxqih21  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(150)

我正在尝试访问application.properties文件中Neo4jConnectionFactory类的值。我可以访问Setup bean类中的属性。但是,我希望能够从Neo4jConnectionFactory访问它们。当我尝试这样做时,我得到了空值。这可能吗?
Applications.properties

neo4j.url = bolt://localhost:7687
neo4j.user = neo4j
neo4j.password = neo4j

SetupBeans.java

@Component
public class SetupBeans {
    @Bean
    public Neo4jClient neo4jClient() {
        return new Neo4jConnectionFactory().build();
    }
}

Neo4jConnectionFactory.java

@Configuration("neo4j")
public class Neo4jConnectionFactory {

    @Value("${neo4j.url}")
    private String url;

    @Value("${neo4j.user}")
    private String user;

    @Value("${neo4j.password}")
    private String password;

    public Neo4jClient build() {
        return new Neo4jClient(getUrl(), getUser(), getPassword());
    }
}

MyApplication.java

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
amrnrhlw

amrnrhlw1#

我想这会自动地在前面加上前缀neo4j

@Configuration("neo4j")

所以@Value语句应该省略前缀,如下所示:

@Value("${url}")
    private String url;

    @Value("${user}")
    private String user;

    @Value("${password}")
    private String password;

相关问题