diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java index 198eba91..7771d381 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/config/DependencyInjectionPostProcessor.java @@ -6,6 +6,7 @@ import com.qqchen.deploy.backend.framework.domain.Entity; import com.qqchen.deploy.backend.framework.dto.BaseDTO; import com.qqchen.deploy.backend.framework.enums.ResponseCode; import com.qqchen.deploy.backend.framework.exception.BusinessException; +import com.qqchen.deploy.backend.framework.exception.DependencyInjectionException; import com.qqchen.deploy.backend.framework.repository.IBaseRepository; import com.qqchen.deploy.backend.framework.service.IBaseService; import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl; @@ -47,29 +48,20 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - // 处理 Controller 的依赖注入 - if (bean instanceof BaseController controller) { - try { + try { + if (bean instanceof BaseController controller) { return injectControllerDependencies(controller, beanName); - } catch (DependencyInjectionException e) { - log.error("Service injection failed for controller: {}", beanName, e); - throw e; - } catch (Exception e) { - log.error("Unexpected error during service injection for controller: {}", beanName, e); - throw new BeanCreationException("Failed to inject service for controller: " + beanName, e); } - } - - // 处理 Service 的依赖注入 - if (bean instanceof BaseServiceImpl service) { - try { + if (bean instanceof BaseServiceImpl service) { return injectServiceDependencies(service, beanName); - } catch (Exception e) { - log.error("Failed to inject dependencies for service: {}", beanName, e); - throw new BeanCreationException("Failed to inject dependencies for service: " + beanName, e); } + } catch (DependencyInjectionException e) { + log.error("Dependency injection failed for bean: {}", beanName, e); + throw e; + } catch (Exception e) { + log.error("Unexpected error during dependency injection for bean: {}", beanName, e); + throw new DependencyInjectionException(ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, new Object[] {beanName, e.getMessage()}, e); } - return bean; } @@ -89,11 +81,11 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp try { IBaseService service = (IBaseService) cachedService; ReflectionUtils.setField("service", controller, service); - log.debug("Successfully injected service {} for controller {}", - service.getClass().getSimpleName(), + log.debug("Successfully injected service {} for controller {}", + service.getClass().getSimpleName(), controller.getClass().getSimpleName()); } catch (Exception e) { - throw new DependencyInjectionException("Failed to inject service field", e); + throw new DependencyInjectionException(ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, new Object[] {entityClass.getSimpleName(), e.getMessage()}, e); } return controller; @@ -159,7 +151,10 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp throw e; } catch (Exception e) { log.error("Unexpected error while injecting dependencies for {}", entityClass.getSimpleName(), e); - throw new DependencyInjectionException("Failed to inject dependencies: " + e.getMessage(), e); + throw new DependencyInjectionException( + ResponseCode.DEPENDENCY_INJECTION_SERVICE_NOT_FOUND, + new Object[] {entityClass.getSimpleName(), e.getMessage()}, + e); } } @@ -187,16 +182,6 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp entityPathCache.clear(); } - public static class DependencyInjectionException extends BeanCreationException { - public DependencyInjectionException(String message) { - super(message); - } - - public DependencyInjectionException(String message, Throwable cause) { - super(message, cause); - } - } - // 辅助方法,用于注入具体的依赖 @SuppressWarnings("unchecked") private , D extends BaseDTO, ID extends Serializable> diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java index f19430f0..2d6b44d9 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/BusinessException.java @@ -7,8 +7,11 @@ import lombok.Getter; public class BusinessException extends RuntimeException { private final ResponseCode errorCode; + private final Object[] args; + private Throwable cause; + public BusinessException(ResponseCode errorCode) { this(errorCode, null); } @@ -18,4 +21,11 @@ public class BusinessException extends RuntimeException { this.errorCode = errorCode; this.args = args; } -} \ No newline at end of file + + public BusinessException(ResponseCode errorCode, Object[] args, Throwable cause) { + super(); + this.errorCode = errorCode; + this.args = args; + this.cause = cause; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java new file mode 100644 index 00000000..3fc51e49 --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/DependencyInjectionException.java @@ -0,0 +1,21 @@ +package com.qqchen.deploy.backend.framework.exception; + +import com.qqchen.deploy.backend.framework.enums.ResponseCode; + +/** + * 依赖注入异常 + */ +public class DependencyInjectionException extends BusinessException { + + public DependencyInjectionException(ResponseCode errorCode) { + super(errorCode); + } + + public DependencyInjectionException(ResponseCode errorCode, Object[] args) { + super(errorCode, args); + } + + public DependencyInjectionException(ResponseCode errorCode, Object[] args, Throwable cause) { + super(errorCode, args, cause); + } +} \ No newline at end of file