我在類和接口之下創建了,但原型bean構造函數沒有被調用。我正在使用@Lookup來創建原型範圍的bean。無法使用@Lookup註解在單例bean中的原型範圍中自動裝入bean
public interface IProtoTypeBean {}
@Component
@Scope(value = "prototype")
public class ProtoTypeBean implements IProtoTypeBean {
public ProtoTypeBean() {
super();
System.out.println("ProtoTypeBean");
}
}
@Component
public class SingleTonBean {
IProtoTypeBean protoTypeBean = getProtoTypeBean();
@Lookup
public ProtoTypeBean getProtoTypeBean(){
return null;
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SingleTonBean.class);
SingleTonBean s1 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p1 = s1.protoTypeBean;
SingleTonBean s2 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p2 = s2.protoTypeBean;
System.out.println("singelton beans " + (s1 == s2));
// if ProtoTypeBean constructor getting called 2 times means diff objects are getting created
}
}
感謝,但沒有奏效,我應該在哪裏定義抽象getProtoTypeBean方法。 –
更新getProtoTypeBean()並將其抽象爲 – Mudassar
我應該把它放在另一個抽象類 –