InjectionPoint.getBean () returns null if the bean is an EJB bean in Java EE 7 (CDI 1.1)

I want to get a bean from a producer method in order to read its properties. Some bean scripts have an EJB Singleton bean.

I simplified my code to focus on the problem.

My simple classifier:

 @Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) public @interface InjectMe {} 

Simple manufacturer:

 @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):

 @Singleton @Startup public class SimpleSingleton { @Inject @InjectMe private String injectMe; @PostConstruct public void init() { System.out.println(injectMe); } 

}

Console output:

Info: ip = [BackedAnnotatedField] @Inject @InjectMe private com.test.ejb.SimpleSingleton.injectMe, bean=null

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 the Singleton bean, but it is not in Java EE 7 . I am using Glassfish 4 application server.

Is this behavior indicated somewhere?

+6
source share
1 answer

Using

 injectionPoint.getMember().getDeclaringClass() 

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

 injectionPoint.getBean().getBeanClass() 
Method

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

If you 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 classic code does not work!

Conclusion: it works on older CDI (Weld) implementations. In some newer Weld (introduced in Payara 161), the behavior has changed. I do not know whether it is intentional or not.

However, the solution should use

 injectionPoint.getMember().getDeclaringClass() 

and annotate bean maker with

 @javax.enterprise.context.ApplicationScoped 

annotations.

+2
source

Source: https://habr.com/ru/post/1240034/


All Articles