Problems when I use instanceof together with @EJB, why is that?
I got two interface-classes (actually three) in a maven project called "interface":@Local
public interface CrudSessionLocal extends CRUDS {
}
public interface ExceptionThrower {
public boolean isThrowException();
public void setThrowException(boolean throwException);
}
I got a bean in a maven project called "server" (that got dependency to "interface"-project) that implements these interfaces:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CrudsTransactionSessionBean implements CrudSessionLocal, ExceptionThrower {
I got a servlet in a maven project called "web-client" (that got dependency to "interface"-project, but not to "server"-project). The servlet uses the bean like this code below, remark that the declaration uses the interface.
@EJB(beanName = "CrudsTransactionSessionBean")
private CrudSessionLocal crudSessionLocal;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if( crudSessionLocal instanceof ExceptionThrower ) {
ExceptionThrower exceptionThrower = (ExceptionThrower)crudSessionLocal;
exceptionThrower.setThrowException(throwException);
}
I thought that use of the interface in the declartion of the bean-member in the servlet is a good practice. For eg I thought that I can reuse the interface.jar and web-client.jar in an other ear but with an other serverlayer like other_server.jar that holds an other bean that does something else as long as the bean is called "CrudsTransactionSessionBean" and implements the interface.
In this case the bean also implements the interface ExceptionThrower. The idea is to check in the servlet if the bean is an instanceof ExceptionThrower, then an exception shall be thrown.
But to my surprise, at runtime, the member-bean is NOT an instance of ExceptionThrower. It is:
"com.myapp.cruds.server.CrudsTransactionSessionBean_d7q6e8_CrudSessionLocalImpl@617fe9e5"
(That sounds like a CrudsTransactionSessionBean to me and that implements ExceptionThrower)
I think I can do it like this, but then I will not acomplish the "loose coupling" between the jar-dependecies.
@EJB(beanName = "CrudsTransactionSessionBean")
private CrudsTransactionSessionBean crudSessionLocal;
Do you guys see why this is happening?
And to accomplish the "loose coupling" how should I solve it?
Btw I use Java 8 and Weblogic 14
Best regards
Fredrik