且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果bean是Java EE 7(CDI 1.1)中的EJB bean,则injectionPoint.getBean()返回null。

更新时间:2021-08-08 03:59:28

使用

injectionPoint.getMember().getDeclaringClass()

在WildFly 10.1.0中为我工作,我还迅速在Payara Server 4.1.1.162 #badassfish(内部版本116)中对其进行了测试。我还对全新的Payara Server 4.1.1.164 #badassfish(内部版本28)进行了测试。但是,我不得不将生产者bean的范围更改为@ApplicationScoped。默认范围无效。就我而言,这甚至很有意义:)

works for me in WildFly 10.1.0 and I also quickly tested it in Payara Server 4.1.1.162 #badassfish (build 116). I also did a test on brand new Payara Server 4.1.1.164 #badassfish (build 28). However,I had to change the scope of the producer bean to @ApplicationScoped. Default scope did not work. In my case, it even makes sense :)



The

injectionPoint.getBean().getBeanClass()

方法在旧的Payara中为我工作,但不适用于新的WildFly 10.1.0.Final和新的Payara Server 4.1.1.164 #badassfish(内部版本28)。

method worked for me in the old Payara, but not in the new WildFly 10.1.0.Final and new Payara Server 4.1.1.164 #badassfish (build 28).

如果您查看Payara,当前的新版本164包含Weld 2.4.0.Final和WildFly 10.1.0Final使用2.3.5.Final版本。在这两个版本中,经典代码均无效!

If you have a look at Payara, the current new version 164 contains Weld 2.4.0.Final and WildFly 10.1.0Final uses version 2.3.5.Final. In both versions, the classical code does not work !

结论是,在较旧的CDI实现(焊接)下,它可以工作。在某些更新的Weld(在Payara 161中引入)中,行为发生了变化。我不知道这是否是故意的。

The conclusion is, on older CDI implementations (Weld), it works. In some newer Weld (introduced in Payara 161), the behavior changed. I do not know if this is intentional or not.

但是,解决方案是使用

injectionPoint.getMember().getDeclaringClass()

并用

@javax.enterprise.context.ApplicationScoped

注释。