如何为一个类实例化多个 CDI/Weld bean?

发布于 2024-08-22 05:30:07 字数 669 浏览 12 评论 0原文

在 Spring 中,可以通过在 xml conf 中定义相应的 bean 来实例化任何类。还可以使用不同的参数为同一类实例化多个 bean...

CDI 中是否也有此类功能,即是否可以使用不同的初始化参数创建同一类的不同实例?

是否也可以在不更改类的情况下创建 bean...我的意思是不添加注释?

添加

让我举个例子。

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

如何实例化同一类的两个实例并使用不同的字段值初始化它们?

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 with different parameters.....

Are the such features in CDI as well, namely is it possible to create different instances of the same class with different initialization parameters?

Is it also possible to create a bean without changing the class....I mean without adding annotation?

ADDED

Let me make 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 can instantiate two instances of the same class and initialize them with different field values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人的歌 2024-08-29 05:30:07

据我所知,有两个选项:

  • 在不进一步了解您的用例的情况下,我认为您要么想要为(模拟)测试或配置问题提供一些替代实现(例如 OrderService 的另一个 PaymentProvider)。这是规范本身支持的,请查看 @Alternative 此处(不要重复我最初的错误并忘记激活 beans.xml 中的替代方案)

  • 获取 Spring 风格的 XML 配置,您可以使用 Seam 3 Config,它提供了如上所述的 XML 配置。顺便说一句,这已经是 JSR 299 的一部分,但由于某种原因已从规范中删除。

Two options as far as I can see:

  • Without further knowledge of your usecase, I assume that you either want to provide some alternative implementation for (mock-) testing or configuration issues (say another PaymentProvider for a OrderService). This is supported by the spec itself, have a look at @Alternative here (and don't repeat my initial mistake and forget to activate alternatives in beans.xml)

  • To get a Spring-style XML-configuration, you can use Seam 3 Config, which provides XML-configuration just as described. BTW, this has been a part of JSR 299, but has been removed from the spec for whatever reason.

执手闯天涯 2024-08-29 05:30:07

有几种方法可以做到这一点。

例如使用@New

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

这会强制容器创建一个新实例,无论 bean 最初具有什么范围。

另一种方法是简单地将 YourBean 定义为 @Dependent 作用域(顺便说一句,如果类根本没有注释,这是当前的默认值)。

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

维持三分热 2024-08-29 05:30:07

您可以轻松使用生产者方法,如果您有多个应由某个方法生成的实例,请使用 CDI 限定符来区分不同类型的实例。

你也可以使用Seam Config来以Spring的方式完成它,但我认为生产者方法是更优雅的方式。
根据您的用例,还有另一个选项是 @Alternative 。

You can easily use producer methods and if you have multiple instances that should be produced by a method use CDI qualifiers to distinguish different types of instances.

Also you can use Seam Config to do it Spring way but i think producer methods are more elegant way of doing this.
depending on your use case there is another option which is @Alternative .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文