@ConfigurationProperties和@PropertySource

在spring boot(版本1.5.1.RELEASE以上)项目中,@ConfigurationProperties注解已将location属性移除,因此导致无法正常给配置类的属性赋值,所以使用其他的替代途径进行配置。可以使用@ConfigurationProperties和@PropertySource这两个标签配合进行注入配置如下:

@Component
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:author.properties")
public class AuthorSettings {
    private String name;
    private String age;
    getter/setter
  }

在注解@PropertySource中指定配置文件路径。

**注意:**在测试中发现,如果application.properties和author.properties文件存在相同属性名,application.properties配置文件会替代author.properties。

你可能感兴趣的:(springboot)