导入 POJO 和注入 EJB 有什么区别?

发布于 2024-12-13 06:03:01 字数 387 浏览 3 评论 0原文

当我尝试在 eclipse 中创建动态 Web 项目时,我在 eclipse 中收到一些通知,告诉我不仅必须在构建中引用 EJB,而且除了我使用的 @EJB 依赖项注入之外还必须导入该 EJB 包。

谁能帮助我澄清为什么需要这三个以及到底发生了什么。我之所以问这个问题,是因为对我来说,仅导入 EJB 包就足以将 EJB 类置于调用类的范围内,这表明我还没有清楚地了解 JEE6 试图通过注释实现的目标。

编辑:澄清一下,这是一个关于 EJB 的一般问题。它实际上正在工作,我只是好奇为什么除了依赖注入之外还需要导入 bean。看起来它们只是在另一个文件(servlet 或 bean 等)中引用 EJB 类的不同方式,但我认为这不是真的,必须有一些特定的原因才能通过导入包来完成注入/bean 的类。

谢谢你,

As I was attempting to create a Dynamic Web Project in eclipse I received some notifications in eclipse telling me that I had to not only reference the EJB in the build but import that EJB package in addition to the @EJB dependency injection I used.

Can anyone help me clarify why all three are needed and what exactly is going on. The reason I ask is because to me importing the EJB package alone would be enough to put the EJB class in scope of the calling class, and this indicates that I do not yet have a clear understanding of what JEE6 is trying to achieve with annotations.

Edit: to clarify, this is a general question about EJB's. It's actually working I'm just curious as to why importing the bean is needed in addition to the dependency injection. It seems like they're both just different ways to reference an EJB class in another file (servlet or bean, etc) but I don't think that is true there has to be SOME specific reason that injection is done with importing of the package/class of the bean.

Thank you,

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

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

发布评论

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

评论(1

岁月静好 2024-12-20 06:03:01

这听起来好像您误解了 Java 的 import 关键字的含义。它不会像PHP的include()、CSS @import、JSP的<那样在当前类中包含该类的源代码/code> 确实如此,或者什么。它不会在代码中注入任何内容。

当您在代码中使用非 FQN 类名时,import 是必需的,以便编译器准确理解您正在引用的类。想象一下,除了当前包之外,您在多个不同的包中都有一个名为 FooService 的类;如果没有导入或显式 FQN,编译器将不知道您指的是哪一个,因此无法执行任何检查代码是否功能正常(即该类已使用正确的方式并以正确的方式访问)字段/方法名称和正确的方法参数等)。

如果您发现 import 关键字由于某些不明显的原因而令人不安,那么您也可以通过 FQN(完全限定名称,从而包括完整的包名称)来声明该类。例如:

@EJB
com.example.business.FooService fooService;

这样您就可以删除 import com.example.business.FooService; 行。

另请参阅:

This sounds like as if you're misinterpreting the meaning of Java's import keyword. It does not include the source code of the class in the current class like as PHP's include(), CSS @import, JSP's <jsp:include> does, or something. It does not inject anything in the code.

The import is mandatory when you're using non-FQN classnames in the code, so that the compiler understoods what class exactly you're referencing. Imagine that you have a class named FooService in multiple different packages other than the current package; without the import or an explicit FQN, the compiler wouldn't know which one you're referring to and thus can't do any checks if the code is functionally fine (i.e. the class is been used and accessed the right way with the right field/method names and the right method arguments, etc).

If you find the import keyword disturbing for some unobvious reason, then you can also just declare the class by a FQN (Fully Qualified Name, thus including the full package name). E.g.:

@EJB
com.example.business.FooService fooService;

This way you can remove the import com.example.business.FooService; line.

See also:

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