我有这个问题,在 Spring Web 应用程序中,当我们声明一个 bean singleton 时,在 bean 原型中,每次我们调用一个单例时,原型 bean 将是相同的,但是如果我想要一个原型组件在控制器内部的 faacde 中,如果其他组件是单例,最后一个组件怎么能成为原型? 架构是这个单一实例控制器>单例外观>原型服务 谢谢。
如果我理解正确,您可以使用许多选项。最受欢迎的:
1.作用域代理。将此注释添加到原型 Bean 中:
@Scope(
value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.TARGET_CLASS)
2.@Lookup方法注入注释:
@Component
public class SingletonFacade {
@Lookup
public PrototypeBean getPrototypeService() {
return null;
}
}
3.对象工厂接口:
@Componenet
public class SingletonFacade {
@Autowired
private ObjectFactory<PrototypeService> prototypeBeanObjectFactory;
public PrototypeBean getPrototypeInstance() {
return prototypeBeanObjectFactory.getObject();
}
}
您可以在此处找到更多信息 - https://www.baeldung.com/spring-inject-prototype-bean-into-singleton