Reputation: 1822
My custom event
public class TestEvent<E> {
private E object;
}
Test class
public class Test{
String message;
}
Class that listen to event has following listener
@Component
public class TestEventListener{
// This listener works
@EventListener
public void testEvent(Test test) {
logger.info("Test Received");
}
// This listener doesn't work
@EventListener
public void testEvent(TestEvent<Test> testEvent) {
logger.info("Received");
}
}
Code to publish the event
@Service
public class TestService{
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void sendMessage(){
Test test = new Test();
// Event is published
applicationEventPublisher.publishEvent(test);
TestEvent<Test> testEvent = new TestEvent(test)
// Event is not published
applicationEventPublisher.publishEvent(testEvent);
}
}
Not sure what is wrong with the code but call never reaches my listener. I am using Spring 4.3.3 RELEASE
Upvotes: 2
Views: 2146
Reputation: 1822
I was able to solve the issue. Code changes are as below:
public class TestEvent<E> implements ResolvableTypeProvider {
private E object;
@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(),
ResolvableType.forInstance(object));
}
}
Upvotes: 2