–在以往我们讲过通过@Value或@ConfigurationProperties的方式注入yml文件中的配置,今天给大家讲一下如何配置多个列表,比如数组的方式注入yml配置,相当于一个List,对特殊业务情况下非常有用,接下来就具体列一下实现步骤。
一、 创建一个配置类
/**
* 配置类
* @author wfeil211@foxmail.com
*/
@Configuration
@EnableConfigurationProperties(TableListPropertie.class)
public class TableListConfig {
}
二、 创建一个属性类
/**
* 属性类
* @author wfeil211@foxmail.com
*/
@Data
@ConfigurationProperties(prefix = "config.table")
public class TableListPropertie {
private List<TableList> tableList;
}
三、 创建一个TableList类
/**
* 参数类
* @author wfeil211@foxmail.com
*/
@Data
public class TableList {
/** 表名 */
private String tableName;
/** 表备注 */
private String tableRemark;
}
四、 yml配置
#配置
config:
#表集合
table:
tableList:
- tableName: "表名"
tableRemark: "表备注"
五、 具体使用方法
@Autowired
private TableListPropertie tableListPro;
@GetMapping("/test")
public void test(){
log.info(tableListPro.getTableList().get(0).getTableName());
}
本次教程到这里就结束了,希望大家多多关注支持(首席摸鱼师 微信同号),持续跟踪最新文章吧~