Reputation: 2096
I have created this EntityListener:
@Slf4j
@Component
public class AListener {
private final ARepository aRepository;
public AListener(ARepository aRepository) {
this.aRepository = aRepository;
}
...
}
that I use here:
@Entity
@EntityListeners(AListener.class)
@Table(name = "CAT")
@NoArgsConstructor
@AllArgsConstructor
public class Cat implements Serializable {
..
}
but I see this error: Class 'AListener' should have [public] no-arg constructor
and in the logs:
Exception Description: Error encountered when instantiating the class [class com.poli.listener.AListener].
Internal Exception: java.lang.InstantiationException: com.poli.listener.AListener]
Upvotes: 6
Views: 6605
Reputation: 1442
EntityListener
is JPA feature, not Spring ones. You don't need to declare a listener as a @Component
, because the JPA provider shall instantiate it.
That's what actually happens here:
AListener
bean and injects ARepository
dependency as expected.AListener
is an entity listener and tries to instantiate it. In order to do it needs no-args constructor (remember that JPA provider knows nothing about Spring and its beans)You can check it by removing the ARepository
dependency and adding some logging statements inside the constructor.
If you do need some Spring beans inside the listener you can make them accessible as static fields in some utility class.
Upvotes: 5