Search code examples
javaejb

EJB, can't get injection in Json response method


I have a problem with my EJB:

public class EntryResponseBuilder extends BaseResponseBuilder {
@EJB
private ApplicationCustomerCache applicationCustomerCache; //null

I have something like this in class that returns a JSON response.

@Singleton
@Startup
public class ApplicationCustomerCache {
    @EJB
    CustomerDao customerDao;
    private Map<Integer, Customer> customerMap;
    private Map<Integer, List<Customer>> applicationToCustomersMap;
    private static Logger log = Logger.getLogger(ApplicationCustomerCache.class);

    @PostConstruct
    public void initialize() {
        try {
            get();
        } catch (Exception e) {
            log.error("ApplicationCustomerCache.init()", e);
        }
    }

    private void get() throws SQLException {

And its working fine, except that when I try to get injection in EntryResponseBuilder, it's empty. One function before, it inject, where I have class with @Stateless. I've searched in Google, but I can not find any solution.


Solution

  • Your dealing with two issues here:

    1. Your're trying to inject an instance of a bean on a POJO, which is not managed by the container;
    2. What's the EJB spec that you're following?

    For both of them, I think the following answers can help you: