100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > spring boot yml 文件的新的配置@value无法读取

spring boot yml 文件的新的配置@value无法读取

时间:2018-09-29 23:10:20

相关推荐

spring boot yml 文件的新的配置@value无法读取

问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null

原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值

上代码:

工具类:

package com.***.***.utils;import org.springframework.beans.factory.annotation.Value;import org.ponent;import system.Decimal;import java.math.BigDecimal;@Componentpublic class RoundCalculationUtil {@Value("${***.round.calculation}")private String calculationWay;public BigDecimal reCalculateAmount(BigDecimal amount){if(calculationWay.equals("currencyround")){return currencyRound(amount);}else {return round(amount);}}public BigDecimal round(BigDecimal amount) {BigDecimal result = amount.setScale(0, BigDecimal.ROUND_DOWN);BigDecimal lastRound2 = amount.setScale(2, BigDecimal.ROUND_DOWN).subtract(result);if (pareTo(new BigDecimal("0.50")) >= 0) {result = result.add(new BigDecimal("1"));}return result;}public BigDecimal currencyRound(BigDecimal amount){BigDecimal result = amount.setScale(2,BigDecimal.ROUND_DOWN);BigDecimal firstRound4=amount.setScale(4,BigDecimal.ROUND_DOWN);BigDecimal lastRound2=firstRound4.subtract(firstRound4.setScale(2,BigDecimal.ROUND_DOWN));if(pareTo(new BigDecimal("0.0005"))>=0){result=result.add(new BigDecimal("0.01"));}return result;}}

调用的地方

@AutowiredRoundCalculationUtil roundCalculationUtil;@RequestMapping(value = "/roundtest", method = RequestMethod.GET)public ResponseData<String> roundTest(@RequestParam(value = "amount", required = true, defaultValue = "100.1111") BigDecimal amount,@RequestParam(value = "roundcalculation", required = false, defaultValue = "currencyround") String roundcalculation) {try {BigDecimal result=roundCalculationUtil.reCalculateAmount(amount);//BigDecimal result=new RoundCalculationUtil ().reCalculateAmount(amount);//will get null from .properties fileDecimalFormat df = new DecimalFormat("0.00");return new ResponseData(NotificationMsg.SUCCESS, df.format(result));} catch (Exception e) {logger.error(e);return new ResponseData(NotificationMsg.FAILED, e);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。