Is it possible to inject EJB implementation and not its interface using CDI?

后端 未结 3 837
故里飘歌
故里飘歌 2021-02-01 09:37

My configuration is: Wildfly 8.2.0, Weld

Is it possible to inject in bean and not in its interface in CDI ?

@Stateless
class Bean implements IBean {
...
         


        
3条回答
  •  孤街浪徒
    2021-02-01 10:00

    Yes you can, but as EJB inject the business view the only business view you are exposing is the @Local view which is the default when you implement an interface (IBean in your case is a local business interface). So, if you want to inject the bean itself, you need to tell the container that you are using the no-interface view.

    In your example, if you still want to implement your interface and inject Bean you should use the @LocalBean annotation which means that the bean exposes a no-interface view:

    @Stateless
    @LocalBean // <-- no-interface view
    class Bean implements IBean {
    ...
    }  
    
    interface IBean {
    ....
    }
    
    @SessionScoped
    class Scoped {
       @Inject
       Bean bean; //Should be OK
    }
    

    Or, If you don't want to implement any interface, then the bean defines by default a No-Interface view:

    @Stateless
    class Bean {
    ...
    }  
    
    @SessionScoped
    class Scoped {
       @Inject
       Bean bean; //OK
    }
    

    See also:

    • What is local/remote and no-interface view in EJB?
    • Defining EJB 3.1 Views (Local, Remote, No-Interface)
    • EJB 3.1 @LocalBean vs no annotation

提交回复
热议问题