Spring Java 配置 : import properties file

标签 spring

如何在使用 Java 配置配置 Spring 时导入属性文件并访问属性。

我想用java做所有事情。有没有办法做到这一点?

我尝试使用 @ImportResource("classpath:config.properties")但没有用。

最佳答案

我已经在我的 @Configuration 上完成了此操作类使用:

@PropertySource(value="classpath:application.properties")

您可以通过多种方式获取属性:
  • 注入(inject) Environment进入需要属性并使用 environment.getProperty("my.property.value") 的配置 bean , 或
  • 使用 @Value 注释属性如概述 here .
  • 关于Spring Java 配置 : import properties file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647887/

    相关文章:

    java - Spring 启动。执行 saveAndFlush 并修改实体结果为未保存的 transient 异常

    spring - "Cannot resolve method"与mockito

    java - Hibernate @OneToMany - child 拥有关系,但只有 parent id

    java - Spring 无法自动注入(inject)构造函数参数

    Spring Boot :Injection of autowired dependencies failed;

    spring - Spring AOP中CGLIB代理相对于动态代理的缺点是什么?

    javascript - Datatables.net 使用 Spring 进行服务器端分页 : How to select single row's ID?

    Intellij 无法解析 Spring bean 范围租户

    java - Spring Boot 访问静态资源缺少 scr/main/resources

    java - 在 Spring 中使用日期创建 JPA 查询