简体   繁体   English

我可以为EJB使用CDI构造函数注入吗?

[英]Can I use CDI constructor injection for EJBs?

I want to do something like this: 我想做这样的事情:

@Stateless
public class GreeterEjb {


    private final Greeter greeter;


    @Inject
    public GreeterEjb(Greeter greeter) {
        this.greeter = greeter;
    }


    public String greet() {
        return greeter.greet();
    }
}

I tried it with Glassfish 3.1.1 and JBoss 7.0.2 with mixed results. 我用Glassfish 3.1.1和JBoss 7.0.2尝试了混合结果。 Under some circumstances it works, under other circumstances it doesn't. 在某些情况下,它可以工作,在其他情况下则不然。 See this thread in the Glassfisch forum if you are interested in the details. 如果您对详细信息感兴趣,请参阅Glassfisch论坛中的此主题

The EJB 3.1 spec , section 4.9.2 Bean Classes says: EJB 3.1规范 ,4.9.2节Bean类说:

The class must have a public constructor that takes no parameters. 该类必须具有不带参数的公共构造函数。

That sounds like constructor injection is not allowed for EJBs. 这听起来像EJB不允许构造函数注入。

BUT the CDI spec says at the start of section 3 that Session Beans are supported by CDI. CDI规范在第3节开头说,会话Bean受CDI支持。 Section 3.2 then talks at length about CDI and EJBs but never mentions anything about constructor injection not working. 然后,第3.2节详细讨论了CDI和EJB,但从未提及有关构造函数注入不起作用的任何内容。 Which makes me think that it should be allowed. 这让我觉得它应该被允许。

So, do the specs allow CDI constructor injection for EJBs or not? 那么,这些规范是否允许为EJB提供CDI构造函数注入?

Kris and Pete Muir have finally convinced me: The EJB must have a public no-arg constructor even if another constructor is used for injection. KrisPete Muir最终说服了我:即使使用另一个构造函数进行注入,EJB也必须有一个公共的无参数构造函数。 Weird to use two constructors at the same time, but it works. 很奇怪,同时使用两个构造函数,但它的工作原理。 Thanks guys. 多谢你们。

Successfully tested on Glassfish 3.1.1, JBoss 7.0.2 and TomEE 1.0.0-beta-2. 在Glassfish 3.1.1,JBoss 7.0.2和TomEE 1.0.0-beta-2上成功测试。

@Stateless
public class GreeterEjb {

    private final Greeter greeter;


    @Inject
    public GreeterEjb(Greeter greeter) {
        this.greeter = greeter;
    }


    // public no-arg constructor required for EJBs
    // injection still works fine with the @Inject constructor
    public GreeterEjb() {
        this.greeter = null;
    }


    public String greet() {
        return greeter.greet();
    }
}

Constructor injection of EJBs is required in Java EE 6 ONLY IF CDI is enabled for the jar. 仅当为jar启用CDI时,Java EE 6中才需要构造函数注入EJB。 If this not working in an appserver, file a bug. 如果这不适用于appserver,请提交bug。

Please also file an issue here - http://java.net/jira/browse/EJB_SPEC - to have the EJB language spec fixed (it's wrong). 还请在此处提交一个问题 - http://java.net/jira/browse/EJB_SPEC - 修复EJB语言规范(这是错误的)。

This is tested in the CDITCK - https://github.com/jboss/cdi-tck/blob/master/impl/src/main/java/org/jboss/cdi/tck/tests/implementation/enterprise/definition/ExplicitConstructorSessionBean.java - but not for no-interface-views, so please raise an issue in https://issues.jboss.org/browse/CDITCK and we can add a test for your case. 这是在CDITCK中测试的 - https://github.com/jboss/cdi-tck/blob/master/impl/src/main/java/org/jboss/cdi/tck/tests/implementation/enterprise/definition/ExplicitConstructorSessionBean .java - 但不适用于no-interface-views,所以请在https://issues.jboss.org/browse/CDITCK中提出一个问题,我们可以为你的案例添加一个测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM