zoukankan      html  css  js  c++  java
  • springboot @Value 类中读取配置文件 .properties null 原因和解决方案

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

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

    上代码:

    工具类:

    package com.***.***.utils;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import system.Decimal;
    
    import java.math.BigDecimal;
    
    @Component
    public 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 (lastRound2.compareTo(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(lastRound2.compareTo(new BigDecimal("0.0005"))>=0){
                result=result.add(new BigDecimal("0.01"));
            }
            return result;
        }
    
    }

    调用处:

    @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 new ResponseData(NotificationMsg.SUCCESS, df.format(result));
            } catch (Exception e) {
                logger.error(e);
                return new ResponseData(NotificationMsg.FAILED, e);
            }
    
        }
  • 相关阅读:
    由1433端口入侵,浅谈sqlserver安全 (转)
    使用 Aircrack-ng 破解 WEP 和 WPA/WPA2 加密的 Wi-Fi 密码。(转)
    ZZmsvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义 .的分析解决办法 (转)
    提高D3js力导向图加载速度(转)
    Asp.Net实现FORM认证的一些使用技巧(转)
    Windows Server 2008 R2 备份和恢复 (转)
    搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
    Beego源码分析(转)
    go语言实现一个简单的登录注册web小程序
    从无线安全到内网渗透(转)
  • 原文地址:https://www.cnblogs.com/zhengshuangliang/p/9013876.html
Copyright © 2011-2022 走看看