JBoss Seam:组件注入 POJO,但不注入会话 Bean

发布于 2024-08-24 17:22:05 字数 705 浏览 3 评论 0原文

我有一个处理登录的 Seam 组件,名称为“authenticator”:

@Name("authenticator")
public class AuthenticatorAction implements Authenticator
{
    @PersistenceContext 
    private EntityManager em;

    @In(required=false)   
    @Out(required=false, scope = SESSION)
    private User user;

    public boolean authenticate(){ ... }

}

这工作得很好,Seam 注入了 EntityManager 实例。但是,一旦我添加 @Stateless 注释,注入就不会发生!在这种情况下,进入 authenticate() 方法时,EntityManager 实例为 null。另一个有趣的注意事项是,对于我拥有的一个单独的有状态会话 bean,只有当我将其设为静态时,才会注入该类中的 Logger 实例。如果我有它非静态的,它不会被注入。这对于记录器来说很好,但是对于像这样的无状态会话 bean,我显然不能为这些组件提供静态成员变量。

我很困惑,因为这个验证器正是 Seam 预订示例中的样子:注入了一个私有成员变量的无状态会话 bean。

有什么想法吗?

I have a Seam component that handles login, with the name "authenticator":

@Name("authenticator")
public class AuthenticatorAction implements Authenticator
{
    @PersistenceContext 
    private EntityManager em;

    @In(required=false)   
    @Out(required=false, scope = SESSION)
    private User user;

    public boolean authenticate(){ ... }

}

This works just fine, Seam injects the EntityManager instance. However, as soon as I add the @Stateless annotation, none of the injection happens! In this case, the EntityManager instance is null upon entry to the authenticate() method. Another interesting note is that with a separate stateful session bean I have, the Logger instance in that class is only injected if I make it static. If i have it non-static, it is not injected. Thats fine for the logger, but for stateless session beans like that, I obviously can't have static member variables for these components.

I'm confused because this authenticator is exactly how it is in the Seam booking example: a stateless session bean with a private member variable being injected.

Any ideas?

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

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

发布评论

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

评论(1

似梦非梦 2024-08-31 17:22:05

我很好奇:

但是,一旦我添加@Stateless注释,注入就不会发生!

所以我希望您的Authenticator接口标记有@javax.ejb.Local或@javax.ejb.Remote。如果没有,那么你的 Stateless 将无法按预期工作。

当您拥有 @Stateless Session bean 时,您必须激活 Seam 拦截器才能启用 @In-jection。类似

pureCharger-jar.jar 
    META-INF
        ejb-jar.xml
        persistence.xml

ejb-jar.xml 的内容如下所示

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
    <interceptors>
        <interceptor>
            <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
        </interceptor>
    </interceptors>
    <assembly-descriptor>
        <interceptor-binding>
            <ejb-name>*</ejb-name>
            <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
        </interceptor-binding>
    </assembly-descriptor>
</ejb-jar>

如果可能,请查看 Seam Security与 JavaOne 的 Dan Allen 合作,他是《Seam in Action》一书的作者。

问候,

I am curious:

However, as soon as I add the @Stateless annotation, none of the injection happens!

So i hope your Authenticator interface is marked with @javax.ejb.Local or @javax.ejb.Remote. If not, so your Stateless will not work as expected.

When you have a @Stateless Session bean, you must activate a Seam interceptor in order to enable @In-jection. Something like

pureCharger-jar.jar 
    META-INF
        ejb-jar.xml
        persistence.xml

ejb-jar.xml is shown as follows

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
    <interceptors>
        <interceptor>
            <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
        </interceptor>
    </interceptors>
    <assembly-descriptor>
        <interceptor-binding>
            <ejb-name>*</ejb-name>
            <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
        </interceptor-binding>
    </assembly-descriptor>
</ejb-jar>

If possible, take a look at Seam Security with Dan Allen, at JavaOne, author of Seam in Action book.

regards,

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