14

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();
}
4

2 回答 2

14

@PostConstruct 必须在实际的 bean 本身上,而不是在 Interface 类上。如果您想强制所有类都实现@PostConstruct 方法,请创建一个抽象类并使@PostConstruct 方法也抽象。

public abstract class AbstractImplementation {

    @PostConstruct
    public abstract init(..);
}

public class ImplementingBean extends AbstractImplementation {
    public init(..) {
        ....
    }
}
于 2013-05-16T14:20:41.117 回答
1

@PostConstruct必须继续 bean java 类本身。我不知道它会在界面上做什么。

你的 XML 中有这个吗?

<context:annotation-config />

这是一些示例代码:@PostConstruct 示例

于 2013-05-16T14:09:00.647 回答