springboot @Value 类中读取配置文件 .properties null 原因和解决方案

摘要:
问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值上代码:工具类:packagecom.***.***.utils;importorg.springframework.beans.factory.annotation.Value;importorg.sprin

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

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

上代码:

工具类:

package com.***.***.utils;

importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.stereotype.Component;
importsystem.Decimal;

importjava.math.BigDecimal;

@Component
public classRoundCalculationUtil {

    @Value("${***.round.calculation}")
    privateString calculationWay;

    publicBigDecimal reCalculateAmount(BigDecimal amount){
        if(calculationWay.equals("currencyround")){
            returncurrencyRound(amount);
        }else{
            returnround(amount);
        }
    }

    publicBigDecimal round(BigDecimal amount) {
        BigDecimal result = amount.setScale(0, BigDecimal.ROUND_DOWN);
        BigDecimal lastRound2 = amount.setScale(2, BigDecimal.ROUND_DOWN).subtract(result);
        if (lastRound2.compareTo(new BigDecimal("0.50")) >= 0) {
            result = result.add(new BigDecimal("1"));
        }

        returnresult;
    }

    publicBigDecimal 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(lastRound2.compareTo(new BigDecimal("0.0005"))>=0){
            result=result.add(new BigDecimal("0.01"));
        }
        returnresult;
    }

}

调用处:

@Autowired
    RoundCalculationUtil 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 file
DecimalFormat df = new DecimalFormat("0.00");
            return newResponseData(NotificationMsg.SUCCESS, df.format(result));
        } catch(Exception e) {
            logger.error(e);
            return newResponseData(NotificationMsg.FAILED, e);
        }

    }

免责声明:文章转载自《springboot @Value 类中读取配置文件 .properties null 原因和解决方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇自适应分辨率SSH常见错误下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java精确计算小数

Java在计算浮点数的时候,由于二进制无法精确表示0.1的值(就好比十进制无法精确表示1/3一样),所以一般会对小数格式化处理. 但是如果涉及到金钱的项目,一点点误差都不能有,必须使用精确运算的时候,就可以使用BigDecimal方法计算. 但是在使用中还需要注意一个问题: //直接使用double类型数据进行运算 System.out.println(0...

BigDecimal类

Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是...

遇到double 数目过大,转String变成科学计数法

问题:   java中,当double数目过大,转出String时,变成了科学记数法的表示。 总结:   1.项目的存储用的是mysql,mysql的类型和java类型之间存在映射关系,以前关注不多。现在总结一下:    类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+...

java BigDecimal实现精确加减乘除运算

加法: b1.add(b2) b2(加数) b1(被加数)减法: b1.subtract(b2) b2(减数) b1(被减数) 乘法: b1.multiply(b2) b2(乘数) b1(被乘数) 除法: b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP) b2(除数) b1(被除数)   import j...

BigDecimal/Long 前后端交互失去精度解决办法

本文主要参考:https://blog.csdn.net/xu622/article/details/84326599   但是个人觉得这个博客里关于问题原因的解释有点问题。 背景 项目测试过程中,测试给我提了一个bug,新增一个BigDecimal类型数据的时候填入   1234567891234567891.12345  然后页面显示为  12345...

踩坑系列—SqlServer批量新增BigDecimal精度问题&amp;lt;foreach collection="list" item="item" separator="," index="index"&amp;gt;&amp;lt;/foreach&amp;gt;

Insert into ALU_KINGDEE_POWDER_STORE(org_code,verify_date,material_code,material_name,brand,storage,type,weight)VALUES (?,?,?,?,?,?,?,?) , (?,?,?,?,?,?,?,?) ; 11:51:55 [qtp20145...