0

I am using a Weld SE container in my JavaFX application (via AfterburnerFX actually). 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? If not, what's a common design pattern to handle this?

Valya
  • 740
  • 7
  • 17

1 Answers1

0

If you want to control instantiation programmatically, you can inject a Provider for your component instead:

//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.

mtj
  • 3,381
  • 19
  • 30
  • What I want is to create a bean at startup asynchronously, so that I can later just inject an existing instance. Declaring `Instance` or `Provider` as I can see delays creating until `get` method is called. – Valya Jun 01 '18 at 08:32
  • @cassio-mazzochi-molin how does it refer to this question? – Valya Jun 01 '18 at 14:32
  • @Valya For asynchronous creation there is no direct way that I am aware of. With the method described in the answer, you can control the bean creation yourself and perform it in a different thread. – mtj Jun 02 '18 at 06:15