友友们,早上好啊。今天继续AI工具实战系列。我将分享最近几个月使用ai工具用于工作的那些事。既是一次知识的分享,又是一次自我的一次总结,也希望自己的一些使用经验可以帮助到大家。下面正文开始。
最近在某智慧航道管理系统的后端开发中,我们团队遇到的典型问题是某些文件代码量太大,比如VesselRouteService.java文件已膨胀至1800行,集成了航道规划、船舶调度、潮汐计算、锚地分配等15个核心业务流程。这种高度耦合的设计导致以下问题的出现,比如:
于是我打算借助CodeBuddy来进行代码梳理和重构目标,首先设立一个目标:
计算在3小时内将该类拆分为5个独立模块,确保以下功能项:
采用CodeBuddy 进行AI辅助重构,基于以下考量:
基础配置:
这里我们导入了部分需要的依赖
// build.gradle片段
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.locationtech.jts:jts-core:1.19.0' // GIS处理
// 其他依赖...
}
主要的核心代码很多,这里只展示部分,免得大家看着费劲。
重构前VesselRouteService核心结构:
@Service
public class VesselRouteService {
@Autowired private TideRepository tideRepository;
@Autowired private VesselRepository vesselRepository;
// 航道规划方法(含潮汐校验)
public RoutePlan planRoute(Vessel vessel, Point destination) {
// 200行复杂逻辑...
TideData tide = tideRepository.findLatest(vessel.getCurrentPort());
if (!isSafeDepth(vessel.getDraft(), tide.getHeight())) {
throw new RoutingException("水深不足");
}
// ...
}
// 船舶调度
public void scheduleVessel(Long vesselId) { /*...*/ }
// 其他12个方法...
}
向CodeBuddy输入核心指令:
首先需要拆分VesselRouteService.java,按功能拆分为:
1. RoutePlanningService(航道规划核心算法)
2. TideValidationService(潮汐校验服务)
3. VesselSchedulingService(船舶调度引擎)
4. AnchorageAllocationService(锚地分配)
5. NavigationSafetyService(航行安全检查)
同时我提出了以下要求:
AI生成的核心模块结构:
TideValidationService.java
@Service
public class TideValidationService {
@Autowired private TideRepository tideRepository;
@Transactional(readOnly = true)
public boolean validateTideCondition(Point location, double vesselDraft) {
TideData tide = tideRepository.findLatestByCoordinates(location);
return vesselDraft <= (tide.getHeight() + tide.getSurge());
}
}
RoutePlanningService.java
@Service
public class RoutePlanningService {
@Autowired private NavigationSafetyService safetyService; // 意外注入
public RoutePlan calculateRoute(Vessel vessel, Point destination) {
// 仅包含基础路径计算,缺少潮汐校验
List<Point> waypoints = calculateShortestPath(vessel, destination);
return new RoutePlan(waypoints);
}
}
看代码后,我发现"航道规划必须包含潮汐校验"逻辑丢失后,于是补充指令:
RoutePlanningService生成航道规划时,必须调用TideValidationService进行水深验证,原逻辑应在同一个事务中完成,确保数据强一致性。
修复后的RoutePlanningService:
@Service
public class RoutePlanningService {
@Autowired private TideValidationService tideService;
@Autowired private NavigationSafetyService safetyService;
@Transactional
public RoutePlan calculateRoute(Vessel vessel, Point destination) {
// 1. 基础路径计算
List<Point> waypoints = calculateShortestPath(vessel, destination);
// 2. 潮汐验证(新增逻辑)
waypoints.forEach(point -> {
if (!tideService.validateTideCondition(point, vessel.getDraft())) {
throw new RoutingException("路径存在水深不足区域");
}
});
// 3. 安全检查
safetyService.checkNavigationHazards(waypoints);
return new RoutePlan(waypoints);
}
}
测试阶段出现循环依赖错误:
The dependencies of some of the beans in the application form a cycle:
┌─────┐
| routePlanningService defined in file [.../RoutePlanningService.class]
| └───┐
| @Autowired tideValidationService
| └───┐
| vesselSchedulingService defined in file [.../VesselSchedulingService.class]
└─────┘
向CodeBuddy咨询解决方案后,采用接口隔离+@Lazy注解:
修改后的依赖结构:
// 新增校验接口
public interface TideValidator {
boolean validate(Point location, double draft);
}
@Service
public class TideValidationService implements TideValidator {
// 实现细节...
}
@Service
public class RoutePlanningService {
@Lazy @Autowired private TideValidator tideValidator; // 接口注入
// ...其他代码
}
可以看出重构的决定是正确的,模块的单独性能都有了比较大的提升,并且代码类也清爽了很多。大家如果有类似的问题,也可以参考我的解决方式。好了,本期的分享就到这里,我们下期见!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。