Search code examples
javajakarta-eeejbwildflycdi

JEE: Can not set <bean> field <service>.<bean> to <bean>$Proxy$


I am relatively new to the JEE universe, so please bear with me.
I cannot get my head around an exception which is being thrown by my local wildfly (version 10.1.0) server.

The exception states:

Can not set <package>.beans.ApplicationBean field <package>.service.rest.ApplicationService.applicationBean to <package>.beans.ApplicationBean$Proxy$_$$_Weld$EnterpriseProxy$

The actual deployment on the application server succeeds, but invoking a REST call generates said exception.
The weird part is: If I deploy the same package a second time, the invoked REST call does not generate the exception and the application works as intended.

Classes in question are:

<package>.beans.ApplicationBean

@Stateless
@LocalBean
public class ApplicationBean {
    @Inject
    private ApplicationDao applicationDao;

    ... some methods ...
}


<package>.service.rest.ApplicationService

@Path("/applications")
@Stateless
@LocalBean
public class ApplicationService {
    @Inject
    private ApplicationBean applicationBean;

    ... methods which use the applicationBean field ...
}

My reasoning behind my usage of annotations in regards to CDI / EJB are:

  • @Stateless because I need transactions in my DAO class (DAO class is listed below for reasons of completeness)
  • @LocalBean because I am trying to inject specific implementation classes and no interfaces


<package>.daos.ApplicationDao

@Stateless
@LocalBean
public class ApplicationDao {
    @PersistenceContext
    private EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    ... method ...
}

I am not sure if it has anything to do with it, but the ApplicationService class is located in a different module than the bean and dao. In the end, together they form an ear file where a shared-module (bean and daos) are the ejbModule and the service is the web-application.

I would very much appreciate some insight - cheers!


Solution

  • This could be a few things but here is my best guess:

    try changing your @Inject to @EJB

    @Stateless
    @LocalBean
    public class ApplicationBean {
        @EJB
        private ApplicationDao applicationDao;
    
        ... some methods ...
    }
    

    <package>.service.rest.ApplicationService

    @Path("/applications")
    @Stateless
    @LocalBean
    public class ApplicationService {
        @EJB
        private ApplicationBean applicationBean;
    
        ... methods which use the applicationBean field ...
    }
    

    If that fixes it, then the issue is likely that the jar file containing the class doesn't have a beans.xml file so it isn't getting picked up by CDI.