问题
Folks,
I am very annoyed by having to re-learn and waste time with this stuff every time a new version of JBoss rolls around.
I have a stateless EJB that is discovered and declared in the JNDI space:
10:01:53,044 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
DTalk/UserManager/local - EJB3.x Default Local Business Interface
DTalk/UserManager/local-com.doctalk.ejb.UserManagerLocal - EJB3.x Local Business Interface
I need to use this EJB in a servlet which is part of a war which is part of the EAR that contains the EJB. I'd like to do it using injection.
When I use the most intuitive notation:
@EJB
private UserManager userManager;
I get an exception in JBoss logs.
When I use a more flowery notation such as:
@EJB( mappedName = "UserManager" )
private UserManager userManager;
Or
@EJB( mappedName = "DTalk/UserManager/local" ) // EAR is called DTalk
private UserManager userManager;
I get no injections errors in jboss but the injected bean is null.
This is maddening and a huge waste of time and makes me question why I don't dump the Eclipse/jboss tools franchise in favor of NetBeans and GlsssFish.
Any insights appreciated.
Thanks.
回答1:
You are trying to inject (a proxy to) the bean instance itself, instead of its interface.
Yet, according to the deployment logging you've shown, you have only declared the bean to be bounded in JNDI via its (local) interface. In order to make the injection happen, you should either declare the variable in which you're injecting as the interface:
@EJB
private UserManagerLocal userManager;
OR declare that a no-interface
view should be created for your bean:
@Stateless
@LocalBean
public class UserManager implements UserManagerLocal {
...
}
after which you can declare the variable as you did earlier:
@EJB
private UserManager userManager;
来源:https://stackoverflow.com/questions/6833935/jboss-6-injecting-ejb-into-servlet