Java 如何让IntelliJ理解Hibernate侦听器是自动连接的?
我有一个Hibernate实体,它使用Java 如何让IntelliJ理解Hibernate侦听器是自动连接的?,java,spring,hibernate,intellij-idea,Java,Spring,Hibernate,Intellij Idea,我有一个Hibernate实体,它使用@EntityListeners注释连接了一个侦听器 @Entity @EntityListeners(value = UpdateListener.class) public class User { //fields } 这个听众看起来像这样 @Slf4j public class UpdateListener { private final TestDep testDep; public UpdateListener(Tes
@EntityListeners
注释连接了一个侦听器
@Entity
@EntityListeners(value = UpdateListener.class)
public class User
{
//fields
}
这个听众看起来像这样
@Slf4j
public class UpdateListener {
private final TestDep testDep;
public UpdateListener(TestDep testDep) {
this.testDep = testDep;
}
@PrePersist
void preCreate(User taxonomy) {
log.info("pre create");
}
@PostUpdate
void postUpdate(User taxonomy) {
log.info("post update");
}
}
我认为这可能是一个问题,因为没有无参数构造函数,但令我惊讶的是,Spring实际上没有问题,它将自动连接构造函数,就像对bean一样<顺便说一下,code>TestDep看起来就是这样
@Component
public class TestDep {}
我的问题是IntelliJ不理解这个构造函数是自动连接的,所以它没有给出任何关于依赖关系来自何处的建议。当能够推断出关系时,通常会有一个带斜线的绿色圆圈
我尝试将构造函数标记为@Autowired
,但这无助于IntelliJ理解发生了什么,事实上,它会引发一个警告(这是有意义的)
如果我将侦听器设置为@Component
,我会得到依赖项提示,但Spring会实例化它两次,一次是通过@Component
,一次是通过@EntityListeners
我还有别的办法吗 只要抑制警告,继续前进…@AlanSereb,你误解了。通过添加
@Autowired
,我仍然没有得到理想的行为。因此,抑制警告对我没有帮助-我最好还是删除@Autowired
。更新了我的问题,希望能让这更清楚。如果你在
@Autowired // WARNING: Autowired members must be defined in valid Spring bean (@Component|@Service|...)
public UpdateListener(TestDep testDep) {
this.testDep = testDep;
}