refactor: 重构服务依赖注入和上下文管理

移除硬编码的服务注入,改为使用缓存机制动态获取Bean
优化上下文类结构,统一服务获取方式
添加PageContent类支持分页数据封装
实现异步数据加载功能
This commit is contained in:
2025-09-12 12:20:15 +08:00
parent fc263288e4
commit 422994efcd
16 changed files with 191 additions and 201 deletions

View File

@@ -1,21 +1,25 @@
package com.ecep.contract.cloud;
import static com.ecep.contract.SpringApp.getBean;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.springframework.beans.BeansException;
import org.springframework.util.StringUtils;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.other.service.SysConfService;
import com.ecep.contract.util.MyStringUtils;
import com.ecep.contract.util.NumberUtils;
@@ -24,18 +28,35 @@ import lombok.Getter;
import lombok.Setter;
public class AbstractCtx {
@Setter
SysConfService confService;
@Setter
@Getter
Locale locale = Locale.getDefault();
private Map<Class<?>, Object> cachedBeans = new HashMap<>();
public <T> T getBean(Class<T> requiredType) throws BeansException {
return SpringApp.getBean(requiredType);
}
@SuppressWarnings("unchecked")
public <T> T getCachedBean(Class<T> requiredType) throws BeansException {
Object object = cachedBeans.get(requiredType);
if (object == null) {
object = getBean(requiredType);
cachedBeans.put(requiredType, object);
}
return (T) object;
}
public SysConfService getConfService() {
if (confService == null) {
confService = getBean(SysConfService.class);
}
return confService;
return getCachedBean(SysConfService.class);
}
public CompanyService getCompanyService() {
return getCachedBean(CompanyService.class);
}
public EmployeeService getEmployeeService() {
return getCachedBean(EmployeeService.class);
}
public boolean updateText(Supplier<String> getter, Consumer<String> setter, String text, MessageHolder holder,

View File

@@ -205,10 +205,6 @@ public class YongYouU8Service implements IEntityService<CloudYu>, QueryService<C
public void initialize(AbstractYongYouU8Ctx ctx) {
ctx.setRepository(repository);
ctx.setCompanyService(companyService);
ctx.setEmployeeService(employeeService);
ctx.setCompanyVendorService(companyVendorService);
ctx.setCompanyCustomerService(companyCustomerService);
}
public Iterable<CloudInfo> findAllCloudYu() {

View File

@@ -1,7 +1,5 @@
package com.ecep.contract.cloud.u8.ctx;
import static com.ecep.contract.SpringApp.getBean;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
@@ -17,7 +15,6 @@ import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.customer.service.CompanyCustomerEntityService;
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.vendor.service.CompanyVendorEntityService;
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
import com.ecep.contract.model.Company;
@@ -34,26 +31,9 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
@Getter
@Setter
YongYouU8Repository repository;
@Setter
CompanyService companyService;
@Setter
EmployeeService employeeService;
@Setter
CompanyCustomerService companyCustomerService;
@Setter
CompanyCustomerEntityService companyCustomerEntityService;
@Setter
CompanyVendorService companyVendorService;
@Setter
CompanyVendorEntityService companyVendorEntityService;
public void from(AbstractYongYouU8Ctx parent) {
repository = parent.repository;
companyService = parent.companyService;
employeeService = parent.employeeService;
companyCustomerService = parent.companyCustomerService;
companyVendorService = parent.companyVendorService;
}
public void initializeRepository(MessageHolder holder) {
@@ -67,50 +47,28 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
}
}
public CompanyService getCompanyService() {
if (companyService == null) {
companyService = getBean(CompanyService.class);
}
return companyService;
}
EmployeeService getEmployeeService() {
if (employeeService == null) {
employeeService = getBean(EmployeeService.class);
}
return employeeService;
return getCachedBean(CompanyService.class);
}
public CompanyCustomerService getCompanyCustomerService() {
if (companyCustomerService == null) {
companyCustomerService = getBean(CompanyCustomerService.class);
}
return companyCustomerService;
return getCachedBean(CompanyCustomerService.class);
}
public CompanyCustomerEntityService getCompanyCustomerEntityService() {
if (companyCustomerEntityService == null) {
companyCustomerEntityService = getBean(CompanyCustomerEntityService.class);
}
return companyCustomerEntityService;
return getCachedBean(CompanyCustomerEntityService.class);
}
public CompanyVendorService getCompanyVendorService() {
if (companyVendorService == null) {
companyVendorService = getBean(CompanyVendorService.class);
}
return companyVendorService;
return getCachedBean(CompanyVendorService.class);
}
public CompanyVendorEntityService getCompanyVendorEntityService() {
if (companyVendorEntityService == null) {
companyVendorEntityService = getBean(CompanyVendorEntityService.class);
}
return companyVendorEntityService;
return getCachedBean(CompanyVendorEntityService.class);
}
boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code, MessageHolder holder, String topic) {
boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code,
MessageHolder holder, String topic) {
if (StringUtils.hasText(code)) {
Employee employee = getEmployeeService().findByCode(code);
if (employee != null) {
@@ -124,8 +82,8 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
return false;
}
boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name, MessageHolder holder, String topic) {
boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name,
MessageHolder holder, String topic) {
if (StringUtils.hasText(name)) {
Employee employee = getEmployeeService().findByName(name);
if (employee != null) {
@@ -139,7 +97,8 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
return false;
}
boolean updateCompanyByCustomerCode(Supplier<Company> getter, Consumer<Company> setter, String customerCode, MessageHolder holder, String topic) {
boolean updateCompanyByCustomerCode(Supplier<Company> getter, Consumer<Company> setter, String customerCode,
MessageHolder holder, String topic) {
Company company = null;
if (StringUtils.hasText(customerCode)) {
CompanyCustomerEntityService customerEntityService = getCompanyCustomerEntityService();
@@ -176,8 +135,8 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
return false;
}
boolean updateCompanyByVendorCode(Supplier<Company> getter, Consumer<Company> setter, String vendorCode, MessageHolder holder, String topic) {
boolean updateCompanyByVendorCode(Supplier<Company> getter, Consumer<Company> setter, String vendorCode,
MessageHolder holder, String topic) {
Company company = null;
if (StringUtils.hasText(vendorCode)) {
CompanyVendorEntityService vendorEntityService = getCompanyVendorEntityService();
@@ -214,5 +173,4 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
return false;
}
}

View File

@@ -1,7 +1,5 @@
package com.ecep.contract.cloud.u8.ctx;
import static com.ecep.contract.SpringApp.getBean;
import java.io.File;
import java.text.NumberFormat;
import java.time.LocalDate;
@@ -30,6 +28,7 @@ import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.ds.company.CompanyFileUtils;
import com.ecep.contract.ds.contract.service.ContractFileService;
import com.ecep.contract.ds.contract.service.ContractFileTypeService;
import com.ecep.contract.ds.contract.service.ContractItemService;
import com.ecep.contract.ds.contract.service.ContractPayPlanService;
import com.ecep.contract.ds.contract.service.ContractService;
@@ -66,16 +65,6 @@ import lombok.Setter;
* 合同上下文
*/
public class ContractCtx extends AbstractYongYouU8Ctx {
@Setter
private ContractService contractService;
@Setter
private ContractItemService contractItemService;
@Setter
private ContractFileService contractFileService;
@Setter
private ContractPayPlanService contractPayPlanService;
@Setter
private SaleTypeService saleTypeService;
@Setter
private VendorCtx vendorCtx;
@Setter
@@ -92,38 +81,25 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
@Getter
@Setter
private int customerEntityUpdateDelayDays = 1;
public ContractService getContractService() {
if (contractService == null) {
contractService = getBean(ContractService.class);
}
return contractService;
}
ContractItemService getContractItemService() {
if (contractItemService == null) {
contractItemService = getBean(ContractItemService.class);
}
return contractItemService;
return getCachedBean(ContractService.class);
}
ContractPayPlanService getContractPayPlanService() {
if (contractPayPlanService == null) {
contractPayPlanService = getBean(ContractPayPlanService.class);
}
return contractPayPlanService;
public ContractItemService getContractItemService() {
return getCachedBean(ContractItemService.class);
}
ContractFileService getContractFileService() {
if (contractFileService == null) {
contractFileService = getBean(ContractFileService.class);
}
return contractFileService;
public ContractPayPlanService getContractPayPlanService() {
return getCachedBean(ContractPayPlanService.class);
}
SaleTypeService getSaleTypeService() {
if (saleTypeService == null) {
saleTypeService = getBean(SaleTypeService.class);
}
return saleTypeService;
public ContractFileService getContractFileService() {
return getCachedBean(ContractFileService.class);
}
public SaleTypeService getSaleTypeService() {
return getCachedBean(SaleTypeService.class);
}
VendorCtx getVendorCtx() {
@@ -1223,8 +1199,8 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
MyDateTimeUtils.pickLocalDate(file.getName()), holder, "生效日期");
}
List<ContractFileTypeLocal> matched = getContractFileService()
.findAllFileTypes(Locale.getDefault().toLanguageTag()).values().stream()
List<ContractFileTypeLocal> matched = getCachedBean(ContractFileTypeService.class)
.findAll(getLocale()).values().stream()
.filter(local -> {
if (payWay == ContractPayWay.PAY) {
return local.getType().isSupportVendor();