Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
04. Mar 2008, 16:13 CET | Link

Hi,

Can I inject a non-@Name annotated SLSB into a Seam JavaBean? At component creation time?

I was hoping that Seam would also fire a @EJB annotation in a JavaBean somewhat magically, but that don't happen.

My business logic is prepackaged without Seam annotations in a JAR file, which resides in an EAR file - I cannot update those. I've thrown in my new Seam WAR into that EAR, and would like to have a reference to an EJB in my JavaBean.

I've tried maaany different ways of using @EJB in my Seam JavaBeans, but no injection event is fired (verified by using setter injection).

I've tried using @In in my Seam JavaBeans, but I cannot (which is perhaps logical) get Seam to find my none-@Name annotated SLSBs. I've played around with the jndiPattern too. Is this supposed to work?

So currently, to get it to work, I'm doing a InitialContext lookup in my @Create method but that doesn't look nice.

br, Jens

7 Replies:
04. Mar 2008, 17:54 CET | Link

You can use spring-like xml file one-by-one in your WAR


  <component name="name" class="SLSB"/>

You can wrap JNDI lookup one-by-one using annotation or one-to-many using EL injection:


@Name("x")
@Stateless
class XWrapper implements WrapperLocal
{
  @EJB
  public XLocal x

  @Unwrap
  public XLocal getX() { return x }

}

then package all wrappers like a second ejb module inside your ear (not war because ejb is not allowed there).


@Name("name")
class EJBLookup
{
  Object lookup(String jndi)
  {
    // lookup code
  }
}

@Name
class JavaBean
{
  @In("#{name.lookup('x')")
  public XLocal x;

}

I didn't test, but I used similar approach sometime.

Thanks,

Rating:  * * * * *
04. Mar 2008, 18:04 CET | Link

...

Between the two last approach, you can use factory:


@Name('factory')
class Factory
{
  private Object lookup(String jndi) { ... }

  @Factory("x")
  public XLocal getX() { return lookup("x"); }

  @Factory("y")
  public YLocal getY() { return lookup("y"); }

}

@Name("name")
class Bean
{
  @In
  private XLocal x;

  @In
  private YLocal y;
}

Thanks,

Rating:  * * * * *
04. Mar 2008, 20:14 CET | Link

Ciro,

Thanks a bunch, three quick beautiful solutions on a problem I've spent far too much time on. Took number one, adding one line per SLSB in my components.xml is totally acceptable.

Added an auto-create too so my client simply can use @In


<component auto-create="true" class="org.domain.dunno.session.ForaBean" name="fora" />

Again, thanks.

Jens

05. Mar 2008, 09:15 CET | Link

Btw, using @In will inject this reference at invocation time - is there any way to inject this reference at component instatiation time instead, using annotations? Like @InAtInstansiationTime?

I know I could do it in component.xml using the 'property' element, but then I'd need to add a 'component' element for every JavaBean needing a EJB reference too.

br, Jens

05. Mar 2008, 09:28 CET | Link

Well, you could use @Create to do foo = Components.getInstance()...

 

If a man speaks in the forest and there is no woman around to hear him, is he still wrong?

05. Mar 2008, 21:35 CET | Link

True, thanks,

But them I get somewhat too close to what I'm trying to get away from: Having to manually lookup EJB references in the @Create method!

05. Mar 2008, 10:58 CET | Link

I tried your nice one-to-many using EL injection - the no-config concept is of course interesting - but I always get a...

javax.faces.el.ReferenceSyntaxException: javax.el.ELException: Error Parsing: #{ejbLookup.lookup('ForaBean')}

...Exception due to the parenthesis - works though if I add a getter method for a lookup property I've hardcoded. Appers that only EL property binding can be done using @In, not EL method binding?

br, Jens