[英]What is the proper way to create a bean asynchronously using Weld SE (CDI)?
I am using a Weld SE container in my JavaFX application (via AfterburnerFX actually). 我在JavaFX应用程序中使用了Weld SE容器(实际上是通过AfterburnerFX)。 A component is initialized (when a dialog window is shown to the user) and it's fields are injected. 组件被初始化(当向用户显示对话框窗口时),并且其字段被注入。 However, creating one of its dependencies takes much time. 但是,创建其依赖项之一会花费很多时间。
Is there an available facility to instantiate a bean asynchronously directly in Weld SE? 是否有可用的工具直接在Weld SE中异步实例化bean? If not, what's a common design pattern to handle this? 如果没有,处理此问题的常见设计模式是什么?
If you want to control instantiation programmatically, you can inject a Provider
for your component instead: 如果要以编程方式控制实例化,则可以为组件注入Provider
:
//Instead of
@Inject MyComponent comp;
// do this:
@Inject Provider<MyComponent> compProvider;
// Usage in code at any given time:
MyComponent comp = compProvider.get();
Note that you will probably get to the alternative Instance
instead of Provider
at some time in the future (or a different answer) which is an extended interface implementing provider and some more useful functions. 请注意,您将来可能会选择替代Instance
而不是Provider
(或其他答案),这是实现提供者和一些更有用功能的扩展接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.