Reputation: 3199
I have a number of beans implementing an interface and I'd like them all to have the same @PostConstruct. I've added the @PostConstruct
annotation to my interface method, then added to my bean definitions:
<bean class="com.MyInterface" abstract="true" />
But this doesn't seem to be working. Where am I going wrong if this is even possible?
edit: I've added the annotation to the interface like this:
package com;
import javax.annotation.PostConstruct;
public interface MyInterface {
@PostConstruct
void initSettings();
}
Upvotes: 15
Views: 7934
Reputation: 176
The @PostConstruct has to be on the actual bean itself, not the Interface class. If you want to enforce that all classes implement the @PostConstruct method, create an abstract class and make the @PostConstruct method abstract as well.
public abstract class AbstractImplementation {
@PostConstruct
public abstract init(..);
}
public class ImplementingBean extends AbstractImplementation {
public init(..) {
....
}
}
Upvotes: 15
Reputation: 12985
@PostConstruct
has to go on the bean java class
itself. I don't know what it will do on an interface.
Do you have this in your XML?
<context:annotation-config />
Here is some example code: @PostConstruct example
Upvotes: 1