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方法注入进去

你可能感兴趣的:(spring,java,spring,boot)