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();

View File

@@ -137,7 +137,7 @@ public class ContractService implements IEntityService<Contract>, QueryService<C
}
// field
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "parentCode");
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "payWay", "parentCode");
// relation
spec = SpecificationUtils.andParam(spec, paramsNode, "group", "kind", "type", "group", "company", "project");

View File

@@ -77,10 +77,6 @@ public abstract class AbstContractRepairTasker extends Tasker<Object> {
return purchaseBillVoucherCtx;
}
public void setContractService(ContractService contractService) {
contractCtx.setContractService(contractService);
}
@Override
protected Object execute(MessageHolder holder) throws Exception {
try {

View File

@@ -31,7 +31,6 @@ public class ContractFilesRebuildAllTasker extends Tasker<Object> {
ContractCtx getContractCtx() {
if (contractCtx == null) {
contractCtx = new ContractCtx();
contractCtx.setContractService(getContractService());
}
return contractCtx;
}

View File

@@ -1,14 +1,11 @@
package com.ecep.contract.ds.project;
import static com.ecep.contract.SpringApp.getBean;
import java.util.Objects;
import org.hibernate.Hibernate;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.cloud.AbstractCtx;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.project.service.ProjectService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.Project;
@@ -18,8 +15,7 @@ import lombok.Setter;
public class ProjectCtx extends AbstractCtx {
@Setter
private ProjectService projectService;
@Setter
CompanyService companyService;
ProjectService getProjectService() {
if (projectService == null) {
@@ -28,13 +24,6 @@ public class ProjectCtx extends AbstractCtx {
return projectService;
}
CompanyService getCompanyService() {
if (companyService == null) {
companyService = getBean(CompanyService.class);
}
return companyService;
}
public boolean updateCustomer(Project project, Company customer, MessageHolder holder) {
boolean modified = false;
if (!Objects.equals(project.getCustomer(), customer)) {

View File

@@ -16,6 +16,8 @@ import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
@@ -27,6 +29,7 @@ import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.ecep.contract.IEntityService;
import com.ecep.contract.PageArgument;
import com.ecep.contract.PageContent;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.other.service.EmployeeLoginHistoryService;
@@ -48,6 +51,8 @@ import lombok.Data;
@Component
public class WebSocketHandler extends TextWebSocketHandler {
private final AuthenticationManager authenticationManager;
private final ObjectMapper objectMapper;
private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);
@@ -78,8 +83,9 @@ public class WebSocketHandler extends TextWebSocketHandler {
}
}
WebSocketHandler(ObjectMapper objectMapper) {
WebSocketHandler(ObjectMapper objectMapper, AuthenticationManager authenticationManager) {
this.objectMapper = objectMapper;
this.authenticationManager = authenticationManager;
}
/**
@@ -317,7 +323,8 @@ public class WebSocketHandler extends TextWebSocketHandler {
JsonNode pageableNode = argumentsNode.get(1);
PageArgument pageArgument = objectMapper.treeToValue(pageableNode, PageArgument.class);
QueryService<?> entityService = (QueryService<?>) service;
return entityService.findAll(paramsNode, pageArgument.toPageable());
Page<?> page = entityService.findAll(paramsNode, pageArgument.toPageable());
return PageContent.of(page);
}
private void sendError(WebSocketSession session, String messageId, String message) {