EJB 3.1:是否允许将Bean注入到容器无法管理的资源中? [英] EJB 3.1: Does it allow injection of beans into resources not managed by the container?

查看:56
本文介绍了EJB 3.1:是否允许将Bean注入到容器无法管理的资源中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JBoss 6.1及其不完全符合EJB 3.1的规范。到目前为止,我无法通过 @EJB 将EJB注入我的Struts动作类(或任何非Java EE容器管理的类)中,但这会是



如果没有完全实现EJB 3.1规范,是否可能在可预见的将来由于性能原因而无法实现?

解决方案

没有容器将无法向非托管对象中注入任何东西。



为了能够注入到对象中,容器需要管理对象的生命周期,或者至少要参与其生命周期的管理,因此容器可以在早期阶段就握住对象进行注入。如果创建和使用对象时从未暴露给容器(就像我想象的Struts动作bean那样),那么容器将永远没有机会向其中注入任何东西。容器不是魔术,它不能仅仅检测整个JVM上创建的对象并对它们做事。



Mikko的答案很好地列出了各种可注射的物体。恐怕除非动作豆是其中之一,否则不要骰子。



现在,说了这么多,隧道尽头就是一盏明灯:它可能会为Struts编写一个处理注入的扩展。 @EJB和@Resource注入与特定的JNDI查找相当直接地对应。扩展可以反射地查找带注释的字段,然后执行相应的JNDI查找并注入结果。 CDI注入更加容易,因为它具有专门用于编写扩展的API。有关执行所有操作的示例,请查看条纹注入富集器,其中添加了对条纹 Web框架的@ EJB,@ Resource和@Inject的支持。 p>

I am using JBoss 6.1 and its not fully EJB 3.1 compliant. As of now, I can't inject an EJB into my Struts action classes (or in any non-Java EE Container-managed class) via @EJB, but will this be possible when the EJB 3.1 specification is fully implemented?

If not, will it be too infeasible to have that due to performance reasons in the foreseeable future?

解决方案

No container will ever be able to inject anything into a non-managed object.

To be able to to inject into an object, the container needs to manage the object's lifecycle, or at least to participate in the management of its lifecycle, so it can get hold of the object at an early stage to do the injection. If an object is created and used without ever being exposed to the container (as i imagine Struts action beans are), then the container never has a chance to inject anything into it. The container is not magic - it can't just detect objects being created all over the JVM and do things to them.

Mikko's answer has a good list of the kinds of objects that will be injectable. Unless actions beans are one of those, no dice, i'm afraid.

Now, having said all that, there is a light at the end of the tunnel: it may well be possible to write an extension for Struts that handles injection. @EJB and @Resource injections correspond fairly straightforwardly to particular JNDI lookups; an extension could reflectively look for annotated fields, then perform the corresponding JNDI lookups and inject the results. CDI injection is even easier, because it has an API specifically aimed at writing extensions. For an example of doing all this, have a look at the Stripes injection enricher, which adds support for @EJB, @Resource, and @Inject to the Stripes web framework.

这篇关于EJB 3.1:是否允许将Bean注入到容器无法管理的资源中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆