从配置文件中取常量值

53 阅读1分钟

获取配置文件常量值

需求

开发中,经常会有一些常量,变动较少,为了避免每次修改都得去java代码中修改,我们可以集中写在某个配置文件中,这样只用修改配置文件就好

方法

在通用的服务中直接配置读取类,类的属性中直接使用value注解获取对应的配置文件中的属性值${配置中的对应key}

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Data
public class ConstantProperties {

   /**
    * 部门用户排序比较位数最大
    */
   @Value("${blade.sort.sortSize}")
   private Integer sortSize;

   /**
    *   部门用户排序比较位数
    */
   @Value("${blade.sort.sortNum}")
   private Integer sortNum;
}

使用的时候直接注入就行

private final ConstantProperties constantProperties;

在使用的地方直接使用get方法获取即可