Can CDI inject standard library POJOs into an EJB?

后端 未结 1 663
[愿得一人]
[愿得一人] 2021-01-18 13:42

I can inject my own POJO into a managed object like this:

import javax.ejb.Stateless;
import javax.inject.Inject;
@Stateless
public class SomeEjb {
    @Inje         


        
相关标签:
1条回答
  • 2021-01-18 14:00

    I can reproduce this with EAP 6.3 as well.

    The problem most likely occurs because of using Java EE 6. The java.util.Date is located in rt.jar and this JAR does not contain a beans.xml file which would enable CDI. You can only inject objects from JARs containing a beans.xml.

    A common workaround is to use a producer method to inject such an object. You have to wirte this producer yourself but you will be able to inject objects from arbitrary classes regardless to which JAR they belong.

    As far as I know the behaviour changed in Java EE 7, where the beans.xml is optional in some cases: https://blogs.oracle.com/theaquarium/entry/default_cdi_enablement_in_java

    Hope that helps.

    0 讨论(0)
提交回复
热议问题