OpenLiberty: Error on Injection, works on TomEE and Wildfly

SOLUTION:

I found the reason of the error, and it is NOT the problem with OpenLiberty. I did not specify any beans.xml, and with that, the bean-discovery-mode defaults to annotated.

Please refer to the following link:

By default, if you specify nothing and there is no beans.xml, the bean discovery mode of ‘annotated’ and not ‘all’ is assumed. (Oracle 2013)


While I was working on this blog, I encountered this error:

[INFO    ] DSRA8203I: Database product name : MySQL
[INFO    ] DSRA8204I: Database product version : 8.0.11
[INFO    ] DSRA8205I: JDBC driver name  : MySQL Connector/J
[INFO    ] DSRA8206I: JDBC driver version  : mysql-connector-java-8.0.11 (Revision: 6d4eaa273bc181b4cf1c8ad0821a2227f116fedf)
[INFO    ] CWWJP9990I: test/wsjpa:wsjar:file:/home/dwuysan/dev/appservers/wlp/usr/servers/test/apps/expanded/test.war/WEB-INF/lib/d9f2b261-b3c6-4001-8a61-0aaebe46aa99.jar!/_testPU login successful
[INFO    ] WELD-000900: 2.4.5 (Final)
[INFO    ] FFDC1015I: An FFDC Incident has been created: "org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private id.co.lucyana.test.services.LogService.em
  at id.co.lucyana.test.services.LogService.em(LogService.java:0)
 com.ibm.ws.container.service.state.internal.ApplicationStateManager 31" at ffdc_18.06.21_14.11.27.0.log
[INFO    ] FFDC1015I: An FFDC Incident has been created: "com.ibm.ws.container.service.state.StateChangeException: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private id.co.lucyana.test.services.LogService.em
  at id.co.lucyana.test.services.LogService.em(LogService.java:0)
 com.ibm.ws.app.manager.module.internal.DeployedAppInfoBase 384" at ffdc_18.06.21_14.11.27.1.log
[INFO    ] CWWJP9990I: test/wsjpa:wsjar:file:/home/dwuysan/dev/appservers/wlp/usr/servers/test/apps/expanded/test.war/WEB-INF/lib/d9f2b261-b3c6-4001-8a61-0aaebe46aa99.jar!/_testPU logout successful
[ERROR   ] CWWKZ0002E: An exception occurred while starting the application test. The exception message was: com.ibm.ws.container.service.state.StateChangeException: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private id.co.lucyana.test.services.LogService.em
  at id.co.lucyana.test.services.LogService.em(LogService.java:0)

The two classes we need to focus on are:

package id.co.lucyana.test.util;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class EntityManagerProducer {
    
    @PersistenceContext(unitName = "testPU")
    @Produces
    private EntityManager em;
}

AND

package id.co.lucyana.test.services;

import id.co.lucyana.test.entity.Log;
import java.util.Collection;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;

@Stateless
@LocalBean
public class LogService {

    @Inject
    private EntityManager em;
       
    public Collection<Log> getLogs() {
        return this.em.createNamedQuery(Log.FIND_ALL, Log.class).getResultList();
    }
}

The thing is, this approach is working on both TomEE and Wildfly.

Any thought?

P.S. I was following someone’s recommendation on the net, of using this approach previously, with the argument that if one should ever change the name of ‘unitName’, it can be done on one single spot. I couldn’t find the reference anymore. I am not sure if this is still a ‘good’ approach. I welcome any comment.

References

Oracle, 2013, ‘Default CDI Enablement in Java EE 7’, Oracle, accessed on 24 June 2018