Question

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();
}
Was it helpful?

Solution

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(..) {
        ....
    }
}

OTHER TIPS

@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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top