问题:在一个工具类中,通过@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);
} }

最新文章

  1. 图文:通过sql server 连接mysql
  2. ZT 第一范式,第二范式,第三范式
  3. WinForm中动态添加控件 出现事件混乱,解决办法记录。
  4. log4net配置文件设置
  5. 简单的TCPIP 客户端 服务器
  6. 锋利的jquery-DOM操作
  7. Learning WCF Chapter2 Service Contracts
  8. 一、Solr综述
  9. MSSQL - 通用存储过程
  10. 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解
  11. 常见的Linux操作系统推荐
  12. Spark1.4从HDFS读取文件运行Java语言WordCounts
  13. 大数据学习之HDFS基本API操作(上)06
  14. linux下编译opencv
  15. JSON for-in 遍历
  16. ZEDGRAPH画图心得,SQL语句构造!!!
  17. 【BZOJ1901】Zju2112 Dynamic Rankings 主席树+树状数组
  18. 2016-2017-2 20155331 实验二《Java面向对象程序设计》实验报告
  19. ES6 对象转Map
  20. Write your first jQuery plugin

热门文章

  1. ORM版,学生信息管理单表查询..
  2. 列表(增删改查)元组tupe
  3. HDU - 6166:Senior Pan(顶点集合最短路&amp;二进制分组)
  4. AtCoder Regular Contest 077 被虐记&amp;题解
  5. push()、shift()与pop()、unshift()、splice()
  6. Cocoapods的操作
  7. ①HttpURLConnection通过报文提交
  8. Cocoa Pod使用总结
  9. 最长的回文串——hdu3068
  10. mysql索引之六:mysql高效索引之覆盖索引