Java EE – how to instantiate multiple CDI / weld beans for a class?

In spring, you can instantiate any class by defining the corresponding bean in XML conf You can also instantiate multiple beans for the same class with different parameters

The same is true for these functions in CDI, that is, can different initialization parameters be used to create different instances of the same class?

Can I also create beans without changing classes I mean no comments?

add to

Let me give an example

<bean id="someBean1" class="org.mm.MyBean">
    <property name="x" value="xx"/>
    <property name="y" value="yy"/>
    <property name="z" value="zz"/>       
</bean>
<bean id="someBean2" class="org.mm.MyBean">
    <property name="x" value="other value"/>
    <property name="y" value="yy2"/>
    <property name="z" value="zz2"/>       
</bean>

How do I instantiate two instances of the same class and initialize them with different field values?

Solution

I can see two options:

>If I don't know more about your use cases, I assume that you either provide some alternative implementations for (simulation) testing or configuration problems (such as another paymentprovider of orderservice) This is supported by the specification itself. Look at @ alternative here (don't repeat my initial error and forget to activate the substitute in beans.xml) > to obtain spring style XML configuration, you can use seam 3 config, which provides the same XML configuration as the configuration Incidentally, this is part of JSR 299, but it has been removed from the specification for whatever reason

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>