动态注入Controller、Service所需的类
This commit is contained in:
parent
8f6de47af3
commit
65e18a90d4
@ -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 {
|
||||
if (bean instanceof BaseController<?, ?, ?, ?> controller) {
|
||||
return injectControllerDependencies(controller, beanName);
|
||||
}
|
||||
if (bean instanceof BaseServiceImpl<?, ?, ?> service) {
|
||||
return injectServiceDependencies(service, beanName);
|
||||
}
|
||||
} catch (DependencyInjectionException e) {
|
||||
log.error("Service injection failed for controller: {}", beanName, e);
|
||||
log.error("Dependency injection failed for bean: {}", 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理 Service 的依赖注入
|
||||
if (bean instanceof BaseServiceImpl<?, ?, ?> service) {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return bean;
|
||||
}
|
||||
|
||||
@ -93,7 +85,7 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp
|
||||
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>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user