[英]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 上尝试了各种解决方案,但都没有成功,特别是:
Checking that I am indeed calling the interface, not the implementation. 检查我确实在调用接口,而不是实现。
Adding @Component to the top of SwitchUserFilter将@Component 添加到 SwitchUserFilter 的顶部
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.