1.创建配置文件application.properties ,上篇文章中我们已经创建了就不需要创建了
2.通过Settings设置对应文件编码为UTF-8,以支持中文
3.在配置文件appication.properties自定义属性
com.study.model.author=li xiao
com.study.model.title=学习教程
4.添加配置工具owner的maven依赖
<!--owner依赖开始-->
<dependency>
<groupId>org.aeonbits.owner</groupId>
<artifactId>owner</artifactId>
<version>1.0.9</version>
</dependency>
<!--owner依赖结束-->
5.创建配置管理ConfigCenter接口
package com.study.model.config; import org.aeonbits.owner.Config; @Config.Sources({"classpath:application.properties"}) public interface ConfigCenter extends Config { @Key("com.study.model.author") @DefaultValue("") String getAuthor(); @Key("com.study.model.title") @DefaultValue("") String getTitle(); }
6.在上面的UserControllerTest添加一个测试方法ownerTest
@Test public void ownerTest() throws Exception{ ConfigCenter cfg = ConfigFactory.create(ConfigCenter.class); System.out.println(cfg.getAuthor()); ConfigCenter instance = ConfigCache.getOrCreate(ConfigCenter.class); System.out.println(instance.getTitle()); Assert.assertEquals("li xiao", cfg.getAuthor()); Assert.assertEquals("学习教程", instance.getTitle()); }
7.点击运行按钮
我出现了一个错误
Error:Failed to load project configuration: cannot parse xml file....
可能是我不小心改了什么东西,删除掉
再 用maven
然后重新运行下就没有错误了
错误参考链接:
https://blog.youkuaiyun.com/aiwoshishen/article/details/80804198