美文网首页
Spring中的application.properties

Spring中的application.properties

作者: 王郁 | 来源:发表于2023-11-15 02:33 被阅读0次

Spring框架在运行时会自动查找application.properties配置文件,在这个文件中进行项目相关配置属性,例如数据库地址密码等。当然也支持自定义属性。

位置

application.properties可以放置在以下目录

  • 当前目录下的/config目录中
  • 当前目录
  • classpath下的 /config包
  • classpath
  • resource文件夹

自定义属性的使用

  1. application.properties添加自定义属性myProperties = myValue
  2. 使用@Value注解即可注入使用
    @Value("${myProperties}")
    String value; //value = "myValue";
  1. 还可以使用@ConfigurationProperties("foo")注入POJO实体
@ConfigurationProperties("foo")
public class Foo{
    private boolean enabled;
    public boolean isEnabled() { ... }
    public void setEnabled(boolean enabled) { ... }
}

application.properties添加属性foo.enabled = true即可

本文于2017年7月11日发表于 https://wycode.cn/blog/spring-properties 转载请注明出处

相关文章

网友评论

      本文标题:Spring中的application.properties

      本文链接:https://www.haomeiwen.com/subject/dwkswdtx.html