gpt4 book ai didi

java - 使用@Inject Instance获取实现特定接口(interface)的EJB和CDI bean

转载 作者:行者123 更新时间:2023-12-02 01:05:56 25 4
gpt4 key购买 nike

在我们的应用程序中,我们有 CDI(@ApplicationScoped 注释)和 EJB(@Stateless 注释)bean,其结构如下:

MyInterface
MyAbstractClass
MyBean (CDI or EJB)

我使用下面的方法来获取我的应用程序中实现 MyInterface 的所有 bean(CDI 和 EJB):

@Inject
Instance<MyIterface> beans;

在这里我看到两个奇怪的事情:

  • 我只获取 CDI bean,而不获取 EJB bean。
  • 当创建直接实现 MyInterface 接口(interface)的 EJB bean 而不扩展 MyAbstractClass 时,在本例中,该 bean 由上面的注入(inject)注入(inject)。

如何通过上面的注入(inject)获取所有 bean、CDI 和 EJB?

最佳答案

引用 EJB 3.2 规范第 4.9.2.1 节:

@Stateless
public class A implements Foo { ... }

@Stateless
public class B extends A implements Bar { ... }

Assuming Foo and Bar are local business interfaces and there is no associated deployment descriptor, session bean A exposes local business interface Foo and session bean B exposes local business interface Bar, but not Foo.

Session bean B would need to explicitly include Foo in its set of exposed views for that interface to apply. For example:

@Stateless
public class A implements Foo { ... }

@Stateless
public class B extends A implements Foo, Bar { ... }
<小时/>

在您的示例中,MyBean定义为 EJB 不公开 MyInterface ,因此不会在 Instance<MyInterface> 处注入(inject).

有两种处理方法:

  • 将 EJB 声明为实现 MyInterface ;
  • 使用 @Local(MyInterface.class) 注释 EJB

@Local 的接近发出警告- 此 EJB 将仅满足那些使用作为注释参数提供的接口(interface)之一的注入(inject)点。您将无法将其注入(inject)

@Inject
MyBean bean;

此外,您将无法将注入(inject)的代理转换为任何其他类型。

关于java - 使用@Inject Instance获取实现特定接口(interface)的EJB和CDI bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com