I run into an issue attempting to directly implement multiple unrelated interfaces in a JavaConfig managed bean:
public class MyServiceImpl implements MyService, ApplicationListener {
...
}
@Bean(scope=DefaultScopes.SINGLETON)
public MyService myService() {
return new MyServiceImpl();
}
While above setup works fine to expose the business interface of my service, it does not make it available as event listener.
When changing (in an attempt to make the event listener portion work) the return type:
@Bean(scope=DefaultScopes.SINGLETON)
public MyServiceImpl myService() {
return new MyServiceImpl();
}
An error is raised that the generated proxy cannot be cast to the expected return type. That's because a JDK proxy is generated for the interfaces, but not the implementation class. That's OK, I do not want the implementation to be exposed in any case.
As workaround, I extended MyService from ApplicationListener and things work as expected. The bean is now found in the bean factory as an event listener a receives events.
It seems a bit odd having to mingle interfaces like this through, there must be a better way to do this.
What's the recommended approach to implement multiple interfaces and have them all exposed within the app context?


