IntelliJ 和 Java Spring 微服务:GitHub Copilot 的生产力技巧
但是,在 Copilot 中,一旦我们添加了 try 块并开始添加 catch 块,它就会自动建议记录器消息并生成整个 catch 块。六个月前,我踏上了探索 GitHub Copilot(一个 AI 驱动的编码助手)的旅程,同时在 IntelliJ IDEA 中从事 Java Spring 微服务项目。在本文中,我将分享我使用 GitHub Copilot(编码伴侣)的旅程和经验,以及它如何提高
你有没有想过一个编码助手可以帮助你更快地编写代码,减少错误,并提高你的整体生产力?在本文中,我将分享我使用 GitHub Copilot(编码伴侣)的旅程和经验,以及它如何提高生产力。本文特别关注 IntelliJ IDE,我们用它来构建基于 Java Spring 的微服务。
六个月前,我踏上了探索 GitHub Copilot(一个 AI 驱动的编码助手)的旅程,同时在 IntelliJ IDEA 中从事 Java Spring 微服务项目。起初,我的体验不是很好。我发现它提供的建议是不恰当的,它似乎阻碍了而不是帮助了开发工作。但我决定坚持使用这个工具,今天,收获了一些好处,还有很大的改进空间。
常见模式
让我们深入了解 GitHub Copilot 发挥重要作用的一些场景。
异常处理
请考虑以下方法:
private boolean isLoanEligibleForPurchaseBasedOnAllocation(LoanInfo loanInfo, PartnerBank partnerBank){
boolean result = false;
try {
if (loanInfo != null && loanInfo.getFico() != null) {
Integer fico = loanInfo.getFico();
// Removed Further code for brevity
} else {
logger.error("ConfirmFundingServiceImpl::isLoanEligibleForPurchaseBasedOnAllocation - Loan info is null or FICO is null");
}
} catch (Exception ex) {
logger.error("ConfirmFundingServiceImpl::isLoanEligibleForPurchaseBasedOnAllocation - An error occurred while checking loan eligibility for purchase based on allocation, detail error:", ex);
}
return result;
}
最初,如果没有 GitHub Copilot,我们将不得不手动添加异常处理代码。但是,在 Copilot 中,一旦我们添加了 try 块并开始添加 catch 块,它就会自动建议记录器消息并生成整个 catch 块。catch 块中的任何内容都不是手动键入的。此外,一旦我们开始输入 logger.error,Co-Pilot 就会自动预填充 else 部分中的其他 logger.error。
单元测试的模拟
在单元测试中,我们经常需要创建模拟对象。考虑我们需要创建对象列表的场景:PartnerBankFundingAllocation
List<PartnerBankFundingAllocation> partnerBankFundingAllocations = new ArrayList<>();
when(this.fundAllocationRepository.getPartnerBankFundingAllocation(partnerBankObra.getBankId(), "Fico")).thenReturn(partnerBankFundingAllocations);
如果我们创建一个对象并将其推送到列表中:
PartnerBankFundingAllocation partnerBankFundingAllocation = new PartnerBankFundingAllocation();
partnerBankFundingAllocation.setBankId(9);
partnerBankFundingAllocation.setScoreName("Fico");
partnerBankFundingAllocation.setScoreMin(680);
partnerBankFundingAllocation.setScoreMax(1000);
partnerBankFundingAllocations.add(partnerBankFundingAllocation);
GitHub Copilot 会自动为其余对象建议代码。如果建议不合适,我们只需要继续按回车键并调整值。
PartnerBankFundingAllocation partnerBankFundingAllocation2 = new PartnerBankFundingAllocation();
partnerBankFundingAllocation2.setBankId(9);
partnerBankFundingAllocation2.setScoreName("Fico");
partnerBankFundingAllocation2.setScoreMin(660);
partnerBankFundingAllocation2.setScoreMax(679);
partnerBankFundingAllocations.add(partnerBankFundingAllocation2);
日志记录/调试语句
GitHub Copilot 还擅长帮助记录和调试语句。请考虑以下代码片段:
if (percentage < allocationPercentage){
result = true;
logger.info("ConfirmFundingServiceImpl::isLoanEligibleForPurchaseBasedOnAllocation - Loan is eligible for purchase");
} else{
logger.info("ConfirmFundingServiceImpl::isLoanEligibleForPurchaseBasedOnAllocation - Loan is not eligible for purchase");
}
在此示例中,所有记录器信息语句均由 GitHub Copilot 自动生成。它考虑了代码条件的上下文,并建议相关的日志消息。
代码注释
它有助于在方法顶部添加注释。在下面的代码片段中,方法上方的注释由 Copilot 生成。我们只需要开始输入 .// This method
// THis method is used to get the loan program based on the product sub type
public static String getLoanProgram(List<Product> products, Integer selectedProductId) {
String loanProgram = "";
if (products != null && products.size() > 0) {
Product product = products.stream().filter(p -> p.getProductId().equals(selectedProductId)).findFirst().orElse(null);
if (product != null) {
String productSubType = product.getProductSubType();
switch (productSubType) {
case "STANDARD":
loanProgram = "Standard";
break;
case "PROMO":
loanProgram = "Promo";
break;
default:
loanProgram = "NA";
break;
}
}
}
return loanProgram;
}
或者,我们可以使用类似 的提示。 Copilot 将添加第二行 .// Q : What is this method doing?// A : This method is used to log the payload for the given api name
// Q : What is this method doing?
// A : This method is used to log the payload for the given api name
public static void logPayload(String apiName, Object payload) {
try {
if (payload != null && apiName != null && apiName.trim().length() > 0) {
ObjectMapper mapper = new ObjectMapper();
String payloadResponse = mapper.writeValueAsString(payload);
logger.info("UnderwritingUtility::logPayload - For api : " + apiName + ", payload : " + payloadResponse);
} else {
logger.error("UnderwritingUtility::logPayload - Either object was null of api name was null or empty");
}
} catch (Exception ex) {
logger.error("UnderwritingUtility::logPayload - An error occurred while logging the payload, detail error : ", ex);
}
}
我们在提示中键入的不同方法的另一个示例:. Copilot 将添加第二行 .// Q : What is this method doing?// A : This method is used to validate the locale from request, if locale is not valid then set the default locale
//Q - Whats below method doing?
//A - This method is used to validate the locale from request, if locale is not valid then set the default locale
public static boolean isLocaleValid(LoanQuoteRequest loanQuoteRequest){
boolean result = false;
try{
if (org.springframework.util.StringUtils.hasText(loanQuoteRequest.getLocale())){
String localeStr = loanQuoteRequest.getLocale();
logger.info("UnderwritingUtility::validateLocale - Locale from request : " + localeStr);
Locale locale = new Locale.Builder().setLanguageTag(localeStr).build();
// Get the language part
String language = locale.getLanguage();
if (language.equalsIgnoreCase("en")){
result = true;
if (!localeStr.equalsIgnoreCase(UwConstants.DEFAULT_LOCALE_CODE)){
loanQuoteRequest.setLocale(UwConstants.DEFAULT_LOCALE_CODE);
}
} else if (language.equalsIgnoreCase("es")){
result = true;
if (!localeStr.equalsIgnoreCase(UwConstants.SPANISH_LOCALE_CODE)){
loanQuoteRequest.setLocale(UwConstants.SPANISH_LOCALE_CODE);
}
}
} else{
result = true;
loanQuoteRequest.setLocale(UwConstants.DEFAULT_LOCALE_CODE);
}
} catch (Exception ex){
logger.error("UnderwritingUtility::validateLocale - An error occurred, detail error : ", ex);
}
return result;
}
结束语
在 IntelliJ 中使用 GitHub Copilot 进行 Java Spring 微服务开发的好处是显著的。它节省了时间,减少了错误,并使我们能够专注于核心业务逻辑,而不是编写重复的代码。当我们开始使用 GitHub Copilot 进行编码之旅时,这里有一些提示:
请耐心等待,并给它一些时间来学习和识别我们遵循的常见编码模式。
密切关注这些建议并根据需要进行调整。有时,它会产生幻觉。
尝试不同的场景,以充分利用 Copilot 的全部功能。
随时了解 Copilot 的改进和更新,以充分利用这一尖端工具。
我们可以将其与 ChatGPT 结合使用。这是一篇关于它如何帮助提高我们的开发效率的文章。
更多推荐



所有评论(0)