动态注入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.dto.BaseDTO;
|
||||||
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
|
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
|
||||||
import com.qqchen.deploy.backend.framework.exception.BusinessException;
|
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.repository.IBaseRepository;
|
||||||
import com.qqchen.deploy.backend.framework.service.IBaseService;
|
import com.qqchen.deploy.backend.framework.service.IBaseService;
|
||||||
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
||||||
@ -47,29 +48,20 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||||
// 处理 Controller 的依赖注入
|
try {
|
||||||
if (bean instanceof BaseController<?, ?, ?, ?> controller) {
|
if (bean instanceof BaseController<?, ?, ?, ?> controller) {
|
||||||
try {
|
|
||||||
return injectControllerDependencies(controller, beanName);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
if (bean instanceof BaseServiceImpl<?, ?, ?> service) {
|
||||||
|
|
||||||
// 处理 Service 的依赖注入
|
|
||||||
if (bean instanceof BaseServiceImpl<?, ?, ?> service) {
|
|
||||||
try {
|
|
||||||
return injectServiceDependencies(service, beanName);
|
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;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,11 +81,11 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp
|
|||||||
try {
|
try {
|
||||||
IBaseService<T, D, ID> service = (IBaseService<T, D, ID>) cachedService;
|
IBaseService<T, D, ID> service = (IBaseService<T, D, ID>) cachedService;
|
||||||
ReflectionUtils.setField("service", controller, service);
|
ReflectionUtils.setField("service", controller, service);
|
||||||
log.debug("Successfully injected service {} for controller {}",
|
log.debug("Successfully injected service {} for controller {}",
|
||||||
service.getClass().getSimpleName(),
|
service.getClass().getSimpleName(),
|
||||||
controller.getClass().getSimpleName());
|
controller.getClass().getSimpleName());
|
||||||
} catch (Exception e) {
|
} 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;
|
return controller;
|
||||||
@ -159,7 +151,10 @@ public class DependencyInjectionPostProcessor implements BeanPostProcessor, Disp
|
|||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Unexpected error while injecting dependencies for {}", entityClass.getSimpleName(), 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();
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
private <T extends Entity<ID>, D extends BaseDTO, ID extends Serializable>
|
private <T extends Entity<ID>, D extends BaseDTO, ID extends Serializable>
|
||||||
|
|||||||
@ -7,8 +7,11 @@ import lombok.Getter;
|
|||||||
public class BusinessException extends RuntimeException {
|
public class BusinessException extends RuntimeException {
|
||||||
|
|
||||||
private final ResponseCode errorCode;
|
private final ResponseCode errorCode;
|
||||||
|
|
||||||
private final Object[] args;
|
private final Object[] args;
|
||||||
|
|
||||||
|
private Throwable cause;
|
||||||
|
|
||||||
public BusinessException(ResponseCode errorCode) {
|
public BusinessException(ResponseCode errorCode) {
|
||||||
this(errorCode, null);
|
this(errorCode, null);
|
||||||
}
|
}
|
||||||
@ -18,4 +21,11 @@ public class BusinessException extends RuntimeException {
|
|||||||
this.errorCode = errorCode;
|
this.errorCode = errorCode;
|
||||||
this.args = args;
|
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