我创建了下面的类和接口,但原型 bean 构造函数没有被调用。我正在使用 @Lookup 来创建原型作用域 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
}
}
将您的代码更改为以下步骤
@Component("protoBean")
@Scope(value = "prototype") public class ProtoTypeBean implements IProtoTypeBean {
和
@Lookup(value="protoBean")
public abstract ProtoTypeBean getProtoTypeBean();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句