无法使用@Lookup 注释在单例bean 的原型范围内自动装配bean
not able to autowire bean in prototype scope in a singleton bean using @Lookup annotation
我在 类 下面创建了接口,但未调用原型 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();
我建议使用原型提供者。
添加maven依赖
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
然后提及 类 由 Spring
管理
ApplicationContext ctx = new AnnotationConfigApplicationContext(SingleTonBean.class, ProtoTypeBean.class);
这里是提供商的用法
@Component
public class SingleTonBean {
@Autowired
private Provider<IProtoTypeBean> protoTypeBeanProvider;
public IProtoTypeBean getProtoTypeBean() {
return protoTypeBeanProvider.get();
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SingleTonBean.class, ProtoTypeBean.class);
SingleTonBean s1 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p1 = s1.getProtoTypeBean();
SingleTonBean s2 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p2 = s2.getProtoTypeBean();
System.out.println("singleton beans " + (s1 == s2));
System.out.println("prototype beans " + (p1 == p2));
}
}
我在 类 下面创建了接口,但未调用原型 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();
我建议使用原型提供者。
添加maven依赖
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
然后提及 类 由 Spring
管理ApplicationContext ctx = new AnnotationConfigApplicationContext(SingleTonBean.class, ProtoTypeBean.class);
这里是提供商的用法
@Component
public class SingleTonBean {
@Autowired
private Provider<IProtoTypeBean> protoTypeBeanProvider;
public IProtoTypeBean getProtoTypeBean() {
return protoTypeBeanProvider.get();
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SingleTonBean.class, ProtoTypeBean.class);
SingleTonBean s1 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p1 = s1.getProtoTypeBean();
SingleTonBean s2 = ctx.getBean(SingleTonBean.class);
IProtoTypeBean p2 = s2.getProtoTypeBean();
System.out.println("singleton beans " + (s1 == s2));
System.out.println("prototype beans " + (p1 == p2));
}
}