• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

EJB3.x XML overriding @EJB Annotation Injection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic