The issue is that there are a few possible BeforeSaveEvent
classes you could be using. Here are some of the Spring classes mocked out to show you the difference (pay attention to the two BeforeSaveEvent
declarations):
open class ApplicationEvent(source: Any): EventObject(source) {}
interface ApplicationListener<T: ApplicationEvent> {
fun onApplicationEvent(event: T)
}
// the Spring Data Neo4j version requires a type
class BeforeSaveEvent<T>(source: Any, val entity: T): ApplicationEvent(source) {}
// the Spring data version does not
class BeforeSaveEvent(source: Any): ApplicationEvent(source) {}
And so if you code to the Spring data version, your code would be this:
open class Something {
open fun beforeSaveEventApplicationListener(): ApplicationListener<BeforeSaveEvent> {
return object : ApplicationListener<BeforeSaveEvent> {
override fun onApplicationEvent(event: BeforeSaveEvent) {
//Do something with event
}
}
}
}
If you code to the Neo4j version (which I think you are, because your tags for your question include spring-data-neo4j-4
), you have to also specify the entity type parameter:
class MyEntity {}
open class Something {
open fun beforeSaveEventApplicationListener(): ApplicationListener<BeforeSaveEvent<MyEntity>> {
return object : ApplicationListener<BeforeSaveEvent<MyEntity>> {
override fun onApplicationEvent(event: BeforeSaveEvent<MyEntity>) {
//Do something with event
}
}
}
}
So you see exactly what the compiler was asking for:
Please give me a type parameter for BeforeSaveEvent
because it is really BeforeSaveEvent<T>
It could be that you imported the wrong class and mean the other BeforeSaveEvent
or you imported the correct BeforeSaveEvent<T>
and didn't adapt to its actual generic type parameter needs.
Also since ApplicationListener
is an interface you don't want ()
after its use because that means you are trying to call a constructor on an interface.
NOTE: it helps in your questions to provide the declaration signatures of relative classes, from the IDE's perspective (have it click through to find the class you are using, it might not be the one you think you are).