Search code examples
javaejbcdi

Can an injectee know its own InjectionPoint?


Is there any way an injectee(EJB, say) know its own injection point?

@Stateless
public class SomeService {

    @PostConstruct
    private void constructed() {
        // do post construction job
        // according to the injectionPoint
    }

    @Context
    private InjectionPoint injectionPoint; // is this possible?
}

Solution

  • If you are injecting your EJB with CDI (using @Inject) and if it has the default scope (no explicit scope or @Dependent).

    You can inject its injection point:

    @Stateless
    public class SomeService {
    
        @PostConstruct
        private void constructed() {
            // do post construction job
            // according to the injectionPoint
        }
    
        @Inject
        private InjectionPoint injectionPoint; // this is possible
    }