项目中遇到的问题
一、spring boot 中 bean 循环引用
问题复现: 三个bean ,都是按构造函数的方式注入,其实现如下
code 01
@Service @AllArgsConstructor public class GoodsServiceImpl implements GoodsService { private final OrderService orderService; }
code 02
@Service @AllArgsConstructor public class OrderServiceImpl implements OrderService { private final UserService userService; }
code 03
@Service @AllArgsConstructor public class UserServiceImpl implements UserService {
private final GoodsService goodsService; }
此时,如果启动项目,报错 。系统提示出现 bean 的循环引用,导致无法启动
┌─────┐ | goodsServiceImpl defined in file [D:\work\stl-shopping\stl-order\target\classes\stl\service\impl\GoodsServiceImpl.class] ↑ ↓ | orderServiceImpl defined in file [D:\work\stl-shopping\stl-order\target\classes\stl\service\impl\OrderServiceImpl.class] ↑ ↓ | userServiceImpl defined in file [D:\work\stl-shopping\stl-order\target\classes\stl\service\impl\UserServiceImpl.class] └─────┘
分析:这里都是采用构造参数的方式注入bean 属性 ,如果使用 @Autowire 系统是否依然报错?
嗯? 没有报错 ,是的,系统正常启动,原因待续
二、项目中浮点计算的陷阱
问题重现:某个实体类统计金额,传入的都是 double 类型的数据,计算总额需要对传入的数据四则运算
public PaymentWithdrawTotalVO(Double income,Double expend,Double refund,Double deposit) { this.subscribeIncome = income == null ? 0d : income; this.subscribeExpend = expend == null ? 0d : expend; this.subscribeRefund = refund == null ? 0d : refund; this.withdrawDeposit = deposit == null ? 0d : deposit; this.remainAmount = this.subscribeIncome + this.subscribeRefund - this.withdrawDeposit - this.subscribeExpend; }
浮点数参与四则运算出现误差,原因还是由于其存储结构导致的。解决办法:涉及浮点数计算使用 BigDecimal 代替
public PaymentWithdrawTotalVO(Double income,Double expend,Double refund,Double deposit) { this.subscribeIncome = income == null ? 0d : income; this.subscribeExpend = expend == null ? 0d : expend; this.subscribeRefund = refund == null ? 0d : refund; this.withdrawDeposit = deposit == null ? 0d : deposit; this.remainAmount = new BigDecimal(subscribeIncome.toString()) .add(new BigDecimal(subscribeRefund.toString())) .subtract(new BigDecimal(withdrawDeposit.toString())) .subtract(new BigDecimal(subscribeExpend.toString())) .doubleValue(); }
三、post 请求部分字段居然是null
在一次使用 post 请求的时候,传入2个参数,userId 字段一直是 null
问题排查:请求的过程,传递的参数都正确,尝试改变下字段的名称
换了个字段,传递参数居然是正确的。那么 userId 为何无法传递参数呢?
尝试多种方式传递,发现一个特点:只有userId 不能传递。,仔细看了下代码,原来别人写了参数过滤器 Filter ,我 @#$#@@$$#$@, 经过一场乌龙,也找了问题。
四、Mavne 报错
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
添加如下配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <fork>true</fork> <!--指定你的jdk地址--> <executable> C:\Program Files\Java\jdk1.8.0_261\bin\javac.exe </executable> </configuration> </plugin> </plugins> </build>
maven 打 jar 包报错:没有主属性
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <fork>true</fork> <!--指定你的jdk地址--> <executable> C:\Program Files\Java\jdk1.8.0_261\bin\javac.exe </executable> </configuration> </plugin> </plugins> </build>
五、spring boot 集成 Elasticsearch 报错
解决办法:在启动类上中加上一行代码
System.setProperty("es.set.netty.runtime.available.processors", "false");
原因不明,参考说是 Redis 与 ES(6.x)冲突导致