springboot 读取配置文件

一个类被spring容器托管以后,想要读取配置文件,很简单:

@Service
public class OrderService {

    @Autowired
    private SkuService skuService;
    @Autowired
    private CouponRepository couponRepository;
    @Autowired
    private UserCouponRepository userCouponRepository;
    @Autowired
    private IMoneyDiscount iMoneyDiscount;
    @Value("${missyou.order.max-sku-limit}")
    private int maxSkuLimit;

    @Value("${missyou.order.pay-time-limit}")
    private Integer payTimeLimit;

这里用@value注解就很容易获取配置文件的内容,但如果注入的属性是静态的,那么又该怎么获取配置文件内容


@Component
public class OrderUtil {
    // B3230651812529
    private static String[] yearCodes;

    @Value("${sleeve.year-codes}")
    public void setYearCodes(String yearCodes) {
        String[] chars = yearCodes.split(",");
        OrderUtil.yearCodes = chars;
    }

通过set方法注入进去

上一篇:Prim & Dijkstra & Floyd 算法实现、联系与区别 && 使用Floyd算法求次短路径


下一篇:项目启动报错怎么办?看看你Spring自动注入用对了嘛?@Autowired XxxService注入问题解决