Spring代理类型不匹配求助:DocumentProcessorApiService Bean类型错误
问题描述
我被这个问题卡了好几天,试了各种办法都没用:在SpringApplication入口类和配置类加了@EnableAspectJAutoProxy(proxyTargetClass = true),application.yml里配了spring.aop.auto=true和spring.aop.proxy-target-class=true,甚至把DocumentProcessorApiService改成了接口形式,还是报这个错:名为'documentProcessorApiService'的Bean预期类型为'DocumentProcessorApiService',实际类型为'com.sun.proxy.$Proxy146'。
相关代码
被报错的Component类
@Component @EnableRetry @Slf4j public class DocumentProcessorApiService { private final RestTemplate documentsApiRestTemplate; private final DocumentsApiProperties documentsApiProperties; private final MulesoftProperties mulesoftProperties; private static final String DOCUMENT_SEARCH_PATH = "/documents/_search"; private static final String PROPS_NAME_VALUE_MAP_FIELD = "propsNameValueMap"; private static final String DOCUMENT_TYPE_FIELD = "Document Type"; private static final String UW_ERROR_REPORT_TYPE = "UW ERROR RPT"; @Autowired public DocumentProcessorApiService(@Qualifier("documentsApiRestTemplate") RestTemplate documentsApiRestTemplate, DocumentsApiProperties documentsApiProperties, MulesoftProperties mulesoftProperties) { this.documentsApiRestTemplate = documentsApiRestTemplate; this.documentsApiProperties = documentsApiProperties; this.mulesoftProperties = mulesoftProperties; } }
注入该Bean的Service类
@Service @Slf4j public class LifeWorkflowEventListener { private final EventConverter eventConverter; private final JmsService jmsService; private final DocumentProcessorApiService documentProcessorApiService; private final LifeApplicationService lifeApplicationService; @Value("${kafka.consumer.workflow-event.action-event-type:}") String actionEventType; @Autowired public LifeWorkflowEventListener(@Qualifier("lifeWorkflow") EventConverter eventConverter, JmsService jmsService, DocumentProcessorApiService documentProcessorApiService, LifeApplicationService lifeApplicationService) { this.eventConverter = eventConverter; this.jmsService = jmsService; log.info("documentProcessorApiService {}", documentProcessorApiService.toString()); this.documentProcessorApiService = documentProcessorApiService; this.lifeApplicationService = lifeApplicationService; } }
解决思路
- 修正
@EnableRetry的使用位置:这个注解应该加在配置类或SpringBoot入口类上,而不是具体的Bean类。放在Bean类上会导致代理逻辑异常,优先把注解移到入口类重试。 - 验证CGLIB依赖:虽然SpringBoot默认包含CGLIB,但如果项目存在依赖排除或冲突,会导致
proxyTargetClass=true不生效,只能生成JDK动态代理。检查maven/gradle依赖树,确认org.springframework.boot:spring-boot-starter-aop和cglib:cglib存在且版本匹配。 - 构造函数注入加
@Lazy:在LifeWorkflowEventListener的构造函数参数DocumentProcessorApiService前添加@Lazy,延迟代理Bean的初始化,避免Spring生成代理前就进行类型匹配:public LifeWorkflowEventListener(@Qualifier("lifeWorkflow") EventConverter eventConverter, JmsService jmsService, @Lazy DocumentProcessorApiService documentProcessorApiService, LifeApplicationService lifeApplicationService) { // 原有代码不变 } - 检查重复Bean定义:确认项目中没有其他地方(如XML配置、其他配置类的
@Bean方法)定义了同名的documentProcessorApiServiceBean,避免出现不同类型的Bean冲突。 - 接口注入的正确姿势:如果已经把
DocumentProcessorApiService改成接口,注入时要用接口类型接收,而非实现类。比如定义DocumentProcessorApi接口,让DocumentProcessorApiService实现它,然后在LifeWorkflowEventListener中注入DocumentProcessorApi类型。 - 调整AOP切面优先级:如果项目有自定义AOP切面,可能和Spring Retry的代理逻辑冲突。给自定义切面加上
@Order(Ordered.HIGHEST_PRECEDENCE)或调整顺序,避免代理生成顺序导致的类型问题。
内容的提问来源于stack exchange,提问作者Daniel Geiselman
相关产品推荐
相关产品推荐

