如果bean是Java EE 7(CDI 1.1)中的EJB bean,则injectionPoint.getBean()返回null。 [英] injectionPoint.getBean() returns null if bean is an EJB bean in Java EE 7 (CDI 1.1)
问题描述
我想从生产者方法中获取bean以便读取其属性。在某些情况下,该bean是 EJB Singleton
bean。
I want to get bean from producer method in order to read its properties. In some scenarios the bean is a EJB Singleton
bean.
我已经简化了代码以关注此问题。 。
I've simplified my code to focus on the problem.
我简单的限定词:
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface InjectMe {}
简单生产者:
@Dependent
public class SimpleProducer {
@Produces
@InjectMe
public String getInjectMe(InjectionPoint ip) {
// ip.getBean() returns null for some reason
return "ip=" + ip + ", bean=" + ip.getBean();
}
}
EJB(单例):
@Singleton
@Startup
public class SimpleSingleton {
@Inject
@InjectMe
private String injectMe;
@PostConstruct
public void init() {
System.out.println(injectMe);
}
}
控制台输出:
信息:ip = [BackedAnnotatedField] @Inject @InjectMe私有com.test.ejb.SimpleSingleton.injectMe,
bean = null
当我更改 Singleton
bean到 CDI
bean一切正常( ip.getBean()
返回的不是null)。即使在 Singleton
bean中,它也可以在 Java EE 6
中工作,但在 Java EE中则不能。 7
。我正在使用Glassfish 4应用程序服务器。
When I change Singleton
bean to CDI
bean everything works fine (ip.getBean()
returns not null). It also worked in Java EE 6
even with Singleton
bean but it does not in Java EE 7
. I am using Glassfish 4 application server.
此行为是否在某处指定?
Is this behavior specified somewhere?
推荐答案
使用
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
注释。
这篇关于如果bean是Java EE 7(CDI 1.1)中的EJB bean,则injectionPoint.getBean()返回null。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!