奇怪的 EJB 行为还是我错过了什么???

发布于 2024-12-28 11:01:50 字数 1461 浏览 4 评论 0原文

这是使用 EJB beans 的一个非常奇怪的行为:

@Local
public interface Provider {
    void test();
}

@Local
public interface ExtProvider extends Provider {
   void test2();
}

public abstract class AbstractProvider implements Provider {
   @Override 
   void test(){ System.out.println("Hello strange " + getTech()); }
   protected abstract String getTech();          
}

public abstract class ExtAbstractProvider extends AbstractProvider implements ExtProvider {
   @Override
   void test2() { System.out.println("Hello from " + getName());}
   @Override
   String getTech() { return "extended EJB";}
   protected abstract String getName();
}
@Stateless
public class ProviderBean extends AbstractProvider {
   @Override
   protected String getTech() { return "EJB";}
}

@Stateless
public class ExtProviderBean extends ExtAbstractProvider {
  @Override
  protected String getName() { return "ext provider";}
}

根据上面的代码,如果我写:

@EJB Provider provider; // should inject an instance of ProviderBean
@EJB ExtProvider extProvider; // should inject an instance of ExtProviderBean

但两者都不起作用!有人会说,在这个例子中,EJB 不知道每次创建哪个实例,因为有两个实例实现了 Provider。奇怪的是:只有当我们将两个 bean 声明为:

public class ExtProviderBean extends ExtAbstractProvider implements ExtProvider
public class ProviderBean extends AbstractProvider implements Provider

在这种情况下,代码才有效。问题是我们必须显式定义实现 bean 实现接口,即使它是从抽象实现中隐式定义的。我错过了什么还是这是一个限制?

Here is a very strange behavior using EJB beans:

@Local
public interface Provider {
    void test();
}

@Local
public interface ExtProvider extends Provider {
   void test2();
}

public abstract class AbstractProvider implements Provider {
   @Override 
   void test(){ System.out.println("Hello strange " + getTech()); }
   protected abstract String getTech();          
}

public abstract class ExtAbstractProvider extends AbstractProvider implements ExtProvider {
   @Override
   void test2() { System.out.println("Hello from " + getName());}
   @Override
   String getTech() { return "extended EJB";}
   protected abstract String getName();
}
@Stateless
public class ProviderBean extends AbstractProvider {
   @Override
   protected String getTech() { return "EJB";}
}

@Stateless
public class ExtProviderBean extends ExtAbstractProvider {
  @Override
  protected String getName() { return "ext provider";}
}

According to the above code, if I write:

@EJB Provider provider; // should inject an instance of ProviderBean
@EJB ExtProvider extProvider; // should inject an instance of ExtProviderBean

but no one of the two works!!! Someone would say that in this example the EJB doesn't know which instance to create each time since there are two instances that implements Provider. And the strange is: it works only if we declare the two beans as:

public class ExtProviderBean extends ExtAbstractProvider implements ExtProvider
public class ProviderBean extends AbstractProvider implements Provider

In this case the code works. The problem is that we have to explicitly define that an implementation bean implements the interface even it is implicitly defined from the abstract implementation. Am I missing something or this is a limitation?

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

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

发布评论

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

评论(1

无人问我粥可暖 2025-01-04 11:01:50

我刚刚通读了这个问题,我认为提供的答案也适用于您的情况。

我只是引用了部分答案以及 EE 规范:

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

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

假设Foo和Bar是本地业务接口并且没有
关联的部署描述符,会话 bean A 公开本地
业务接口Foo和会话bean B暴露本地业务
接口 Bar,但不是 Foo。
会话 bean B 需要显式地
将 Foo 包含在其公开视图集中,以便应用该接口。

I just read through this SO question and I think the provided answer also applies to your case.

I am just citing a part of the answer and thereby the EE spec:

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

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