Spring中的application.properties

2017-07-11Backend
Spring
Spring

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

<!--more-->

位置

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即可

评论区

暂无评论