05-通过owner获取自定义组件

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.csdn.net/aiwoshishen/article/details/80804198

 

 
 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值