动态注入Controller、Service所需的类

This commit is contained in:
dengqichen 2024-11-29 18:13:26 +08:00
parent 8f6de47af3
commit 65e18a90d4
3 changed files with 49 additions and 33 deletions

View File

@ -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<T, D, ID> service = (IBaseService<T, D, ID>) 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 <T extends Entity<ID>, D extends BaseDTO, ID extends Serializable>

View File

@ -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;
}
}
public BusinessException(ResponseCode errorCode, Object[] args, Throwable cause) {
super();
this.errorCode = errorCode;
this.args = args;
this.cause = cause;
}
}

View File

@ -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);
}
}