简体   繁体   中英

EJB3 instantiation

I got some propably trivial question.

If I got defined EJB3+ interface, lets say it's remote like that:

@Remote
public class FooServiceRemote {
   void foo();
}

and one implementation

@Stateless
public class FooService implements FooServiceRemote {
   void foo() { ... }
}

How does the application server by default resolves what implementation to use (and call via proxy) if he knows only @EJB annotation for dependency injection like on interface:

public class SomeClass {
   @EJB
   private FooServiceRemote fooService;
}

Is it done by reflection (shortening name of interface)? Or he scans possible implementations of such interface, choosing one. Or.. ? And what if I want to create more implementations of one interface, is it possible and how to specify what implementation should be instantiated (maybe it is possible via some argument of annotation).

Thanks:-)

In the rare case that you need to have two beans implementing the same interface (not a good practice), you can name them and choose which one you want by name.

@Stateless(name="FooService1")
public class FooService1 implements FooService { }

@Stateless(name="FooService2")
public class FooService2 implements FooService { }

public class SomeClass {
   @EJB(beanName="FooService1") 
   private FooService fooService;
}

Other possible approaches are looking it up using JNDI or the mappedName property. See the javadoc for EJB annotation here: http://download.oracle.com/javaee/6/api/javax/ejb/EJB.html

Just a fix,

@Remote
interface FooServiceRemote {
   void foo();
}

@Stateless
public class FooService implements FooServiceRemote {
   void foo() { ... }
}

With this, application server knows which classes implements the specified interface.

If you have two classes, you must specifify which class do you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM