wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes EJB3.x XML overriding @EJB Annotation Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3.x XML overriding @EJB Annotation Injection" Watch "EJB3.x XML overriding @EJB Annotation Injection" New topic
Author

EJB3.x XML overriding @EJB Annotation Injection

Amar Panigrahy
Greenhorn

Joined: Feb 05, 2012
Posts: 1
Hi everybody,
I am a new bee for EJB3.x which have the ability of dependency Injection using annotations. Please let me know the solution for the following problem
I created 3 Stateless Session beans implementing same Local Interface as shown below:
1) SessionBean1
2) SessionBean2
3) SessionBean3

I am trying to inject SessionBean2 Object through @EJB Annotation and then override it using XML ejb-jar.xml using <ejb-local-ref>.
My question is since all 3 Session beans implement the same interface, am getting following exception message:

Exception message:
Cannot resolve reference Local ejb-ref name=m1,Local 3.x interface =com.di.MyLocalSession,ejb-link=null,lookup=null,mappedName=,jndi-name=,refType=Session because there are 3 ejbs in the application with interface com.di.MyLocalSession

So to remove the ambiguity i added beanName property in SessionBean1 also as shown below:
@EJB(name="m1",beanName="MyEjb2").

Now my requirement is, i want to override the implementation object to MyEjb3 in ejb-jar.xml using <ejb-local-ref>. But it is still injecting MyEjb2 object as if it is not overriden by xml.

Please any kind of help is appreciable.

Entire Code is posted below:

//---SessionBean1---
package com.di;

import javax.ejb.Stateless;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.ejb.Local;

@Stateless(name="MyEjb1")
@Local
public class SessionBean1 implements MyLocalSession
{
@EJB(name="m1",beanName="MyEjb2")// Requirement is to override this annotation with MyEjb3 Object using ejb-jar.xml
MyLocalSession s2;

public void greeting(PrintWriter out) {
out.println("SessionBean 1");
s2.greeting(out);
}
}

//---SessionBean2---
package com.di;

import javax.ejb.Stateless;
import java.io.PrintWriter;
import javax.ejb.Local;

@Stateless(name="MyEjb2")
@Local
public class SessionBean2 implements MyLocalSession
{

public void greeting(PrintWriter out) {
out.println("SessionBean 2");
}
}

//---SessionBean3---
package com.di;

import javax.ejb.Stateless;
import java.io.PrintWriter;
import javax.ejb.Local;

@Stateless(name="MyEjb3")
@Local
public class SessionBean3 implements MyLocalSession
{

public void greeting(PrintWriter out) {
out.println("SessionBean 3");
}
}


//---Local Interface---
package com.di;

import javax.ejb.Local;
@Local
public interface MyLocalSession {
public void greeting(java.io.PrintWriter out);
}

//---ejb-jar.xml---
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<enterprise-beans>
<session>
<ejb-name>MyEjb1</ejb-name>
<local>com.di.MyLocalSession</local>
<ejb-class>com.di.SessionBean1</ejb-class>
<session-type>Stateless</session-type>
<ejb-local-ref>
<ejb-ref-name>m1</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.di.MyLocalSession</local>
<ejb-link>MyEjb3</ejb-link>
<injection-target>
<injection-target-class>com.di.SessionBean1</injection-target-class>
<injection-target-name>s2</injection-target-name>
</injection-target>
</ejb-local-ref>

</session>

<session>
<ejb-name>MyEjb2</ejb-name>
<local>com.di.MyLocalSession</local>
<ejb-class>com.di.SessionBean2</ejb-class>
<session-type>Stateless</session-type>
</session>

<session>
<ejb-name>MyEjb3</ejb-name>
<local>com.di.MyLocalSession</local>
<ejb-class>com.di.SessionBean3</ejb-class>
<session-type>Stateless</session-type>
</session>

</enterprise-beans>
</ejb-jar>

//---SessionClient---

package com.di.client;

import com.di.MyLocalSession;
import java.io.IOException;
import javax.ejb.EJB;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

@WebServlet(name = "SessionClient", urlPatterns = "*.do")
public class SessionClient extends HttpServlet {

@EJB(beanName="MyEjb1")
MyLocalSession s1;
PrintWriter out;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
out = resp.getWriter();
s1.greeting(out);
out.close();
} catch (Exception ex) {
System.out.println("Exception occurred: " + ex.getMessage());
}
}
}

hitting the url : http://localhost:8080/EJBDependencyInjection/*.do gives me the following output:
Output
SessionBean 1
SessionBean 2

I need
SessionBean 1
SessionBean 3


Glassfish 3 Server Console:
INFO: Portable JNDI names for EJB MyEjb3 : [java:global/EJBDependencyInjection/MyEjb3, java:global/EJBDependencyInjection/MyEjb3!com.di.MyLocalSession]
INFO: Portable JNDI names for EJB MyEjb2 : [java:global/EJBDependencyInjection/MyEjb2, java:global/EJBDependencyInjection/MyEjb2!com.di.MyLocalSession]
INFO: Portable JNDI names for EJB MyEjb1 : [java:global/EJBDependencyInjection/MyEjb1!com.di.MyLocalSession, java:global/EJBDependencyInjection/MyEjb1]
INFO: Loading application EJBDependencyInjection at /EJBDependencyInjection
INFO: EJBDependencyInjection was successfully deployed in 4,219 milliseconds.

Thanks in Advance

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB3.x XML overriding @EJB Annotation Injection
 
Similar Threads
OpenEJB, DI Overriding annotation with deployment descriptor
ejb-local-ref and ejb-ref
Clarification on a mock question about Session Bean
xdoclet problem while generating ejb-jar.xml
Error while deploying ear in WEBLOGIC 8.1.3