I recently migrated from Spring to CDI + EJB, I tried to inject my dependencies directly into the builder just like I did in Spring and how it is supported by the CDI but in an EJB:
@Stateless
public class UserService {
private UserDao userDao;
@Inject
public UserService(UserDao userDao) {
this.userDao = userDao;
}
...
}
And I came across the EJB specification that forces you to create a constructor with no arguments, so that the above code does not work until that constructor is created.
After creating the second constructor (without arguments) and running the application I realized that the application used the constructor with arguments in the injections of this EJB having behavior the way I expected.
My question is: would my EJBs built in this way one day be exposed to other systems via lookup / JNDI would they use the constructor without arguments or would they go through the other constructor by injecting the dependencies?
And also no less important: the application server instance in memory, in this case I have two constructors, EJBs with dependencies injected and others without dependencies?
The first case I know is because the CDI does the management and instantiating according to the demand, but the second case of instantiating EJBs without the dependencies (using the constructor without arguments) I do not know to say what is behavior. Mainly because I put System.out
on both constructors and the two were called me worrying because there would be unnecessary objects being managed by the server.