简体   繁体   English

Spring 引导:无法将 bean 'auditLogDao' 作为 'AuditLogDao' 注入,因为它是 JDK 动态代理

[英]Spring Boot: The bean 'auditLogDao' could not be injected as a 'AuditLogDao' because it is a JDK dynamic proxy

I am getting the following error in a Spring Boot project on which I work:我在我工作的 Spring 引导项目中收到以下错误:

The bean 'auditLogDao' could not be injected as a '{redactedpathwithcorporatename}.AuditLogDao' because it is a JDK dynamic proxy that implements: org.springframework.data.jpa.repository.JpaRepository无法将 bean 'auditLogDao' 作为 '{redactedpathwithcorporatename}.AuditLogDao' 注入,因为它是一个 JDK 动态代理,它实现了:org.springframework.data.jpa.repository.JpaRepository

Action:行动:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.考虑将 bean 作为其接口之一注入或通过在 @EnableAsync 和/或 @EnableCaching 上设置 proxyTargetClass=true 来强制使用基于 CGLib 的代理。

I have tried a variety of solutions on StackOverflow without success, specifically:我在 StackOverflow 上尝试了各种解决方案,但都没有成功,特别是:

  1. Checking that I am indeed calling the interface, not the implementation. 检查我确实在调用接口,而不是实现。

  2. Adding @Component to the top of SwitchUserFilter将@Component 添加到 SwitchUserFilter 的顶部

  3. Changing @Resource to @Autowired.将@Resource 更改为@Autowired。

AuditLogDao.java AuditLogDao.java

public interface AuditLogDao extends JpaRepository<AuditLog, String> {}

AuditLogService.java AuditLogService.java

public interface AuditLogService {
    AuditLog save(final AuditLog auditLog);
}

AuditLogServiceImplementation.java AuditLogServiceImplementation.java

public class AuditLogServiceImplementation implements AuditLogService{

    @Resource private AuditLogDao auditLogDao;

    @Override
    public AuditLog save(AuditLog auditLog) {
        return auditLogDao.save(auditLog);
    }
}

The file where I actually want to use the service to save information我实际要使用该服务保存信息的文件

SwitchuserFilter.java SwitchuserFilter.java

public class SwitchUserFilter
        extends org.springframework.security.web.authentication.switchuser.SwitchUserFilter {
    @Resource AuditLogService logService;
'''
        logService.save(auditLog);
'''
}

I am relatively new to Spring Boot, so an explanation of why it fixes the problem would be appreciated.我对 Spring Boot 相对较新,因此请解释一下它解决问题的原因。

I believe the following code will solve your problem.我相信下面的代码会解决你的问题。 Add it to the AuditLogServiceImplementation and remove the @Resource annotation from the auditLogDao.将其添加到AuditLogServiceImplementation并从 auditLogDao 中删除@Resource注释。

@Autowired
private ListableBeanFactory beanFactory;

@EventListener({ContextRefreshedEvent.class})
void contextRefreshedEvent() {
    auditLogDao = beanFactory.getBean(AuditLogDao.class);
}

You can do a similar trick in the filter too, whatever more comfortable for you.您也可以在过滤器中执行类似的技巧,无论您觉得更舒服。

I don't know what is the exact problem, but it's some kind of circular-dependency-like issue.我不知道确切的问题是什么,但这是某种类似循环依赖的问题。 So by manually importing any bean which is affected in this loop, you can resolve the loop.因此,通过手动导入在此循环中受影响的任何 bean,您可以解决该循环。 You will set this one particular dependency AFTER Spring had created all of the other beans.在 Spring 创建了所有其他 bean 之后,您将设置这个特定的依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC 到 SpringBoot 2:bean &#39;xyz&#39; 无法作为 &#39;com..Abc&#39; 注入,因为它是一个 JDK 动态代理,它实现: - Spring MVC to SpringBoot 2: The bean 'xyz' could not be injected as a 'com..Abc' because it is a JDK dynamic proxy that implements: 无法将 bean 作为“类型”注入,因为它是一个 JDK 动态代理,它实现了:reactor.fn.Consumer - The bean could not be injected as a 'Type' because it is a JDK dynamic proxy that implements: reactor.fn.Consumer 在Spring Boot应用程序中注入的Spring Bean为NULL - Injected Spring Bean is NULL in Spring Boot Application Spring请求范围bean代理bean无法注入 - Spring request scope bean proxy bean can not be injected 动态注入 JDK 动态代理作为 spring bean - 但前提是没有其他实现可用 - Dynamically injecting a JDK dynamic proxy as spring bean - but only if no other implementation is available 找不到的 Bean Spring 启动 - Bean that could not be found Spring Boot 强制Spring Boot 2使用JDK代理失败 - Forcing Spring Boot 2 to use JDK Proxy failed Spring Boot 在 Java 中使用 JDK 或 CGLIB 动态代理时如何调试? - How to debug when a JDK or CGLIB dynamic proxy will be used in Java by Spring Boot? 无法创建配置,因为找不到 Bean 验证提供程序 - Vaadin 14 Spring Boot - Unable to create a Configuration, because no Bean Validation provider could be found - Vaadin 14 Spring Boot Spring JDK动态代理和CGLIB - 实现细节 - Spring JDK Dynamic Proxy and CGLIB - implementation details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM