how to instantiate more than one CDI/Weld bean for one class?

后端 未结 3 422
小蘑菇
小蘑菇 2021-01-13 00:38

In Spring it was possible to instantiate any class by defining the corresponding bean in xml conf. It was also possible to instantiate more then one bean for the same class

3条回答
  •  不思量自难忘°
    2021-01-13 01:20

    there are a few ways to do that.

    E.g. use @New

    private @Inject @New YourBean yb; private @Inject @New YourBean yb2;

    This forces the container to create a new instance, regardless what Scope the bean originally had.

    Another way would be to simply define YourBean as being @Dependent scoped (which is btw (currently) the default if a class is not annotated at all).

提交回复
热议问题