@ConfigurationProperties注解取消location属性

当我正在自学如何自定义properties配置文件,为了防止不必要的麻烦,重新创建了一个新的properties文件

fu.properties 

然后在自定义的配置类中引入fu.properties/

但由于我采用的是spring boot 2.0.6版本,@ConfigurationProperties注解中只有一个prefix属性,没有网上说的localtions属性,无法找到对应的fu.properties文件。

于是我开始百度,网上了解到在spring boot 1.5版本之前在@ConfigurationProperties注释中有两个属性:

locations:指定配置文件所在的位置

prefix:指定配置文件中键名称的前缀

解决办法就是在自定义配置类中添加注解@PropertySource("classpath:fu.properties")

具体列子:

@ConfigurationProperties(prefix="fu")
@PropertySource("classpath:fu.properties")
@Component//把普通的pojo实例话到spring容器中,相当于配置文件中的<bean id="" class=""/>
public class FuProperties {
//
@Value("${com.fu.title}")
private String title;
@Value("${com.fu.description}")
private String description;

geting,seting方法略。。。。。。。

}

然后在controller类中通过注解@Autowired自动注入,就可以了

@Autowired
private FuProperties fu;

不过当我自定义的properties中的参数为中文的时候,在网页上显示的是乱码,如果是阿拉伯数字或者英文的话可以正常显示

不知道大家在学自定义的properties文件的时候有没有碰到这种问题

posted @ 2018-10-29 07:54  含苞待放会有时  阅读(671)  评论(0编辑  收藏  举报