EJB 3.1 依赖注入失败
我创建了一个像这样的无状态会话 bean:
@WebServlet(name = "ProductController", urlPatterns = {"/ProductController"})
public class ProductController extends HttpServlet {
@EJB
private ProductFacadeBean productBean;
}
@Stateless
public class ProductFacadeBean extends AbstractFacade<Product> implements ProductFacadeLocalInterface {
@PersistenceContext(unitName = "OnlineStorePU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public ProductFacadeBean() {
super(Product.class);
}
}
@Local
public interface ProductFacadeLocalInterface {
void create(Product product);
void edit(Product product);
void remove(Product product);
Product find(Object id);
List<Product> findAll();
List<Product> findRange(int[] range);
int count();
}
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
问题:
错误是什么?如何 解决了吗?
<块引用>javax.naming.NamingException:查找 失败了 'java:comp/env/Controller.ProductController/productBean' 在 SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NamingException: 解决“Remote”的 Ejb 异常 ejb 引用 名称=Controller.ProductController/productBean,Remote 3.x 接口 =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' 。实际(可能是内部)远程 用于查找的 JNDI 名称是 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [根异常是 javax.naming.NamingException:查找 失败了 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' 在 SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean 未找到]]]
完整的异常附在此处:
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1392: Error instantiating servlet class Controller.ProductController
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class Controller.ProductController
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session into class Controller.ProductController
root cause
javax.naming.NamingException: Lookup failed for 'java:comp/env/Controller.ProductController/productBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]]
root cause
javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]
root cause
javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]
root cause
javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1 logs. GlassFish Server Open Source Edition 3.1
- 当我创建会话时 实体bean使用netbeans吧 为我们创造了抽象的外观? 目的是什么?我知道门面 模式是作为接口 路由请求 ?
- ejb-jar.xml 有什么用?
所有设置均为默认设置。 ejb bean 是在 java ee 6 Web 应用程序中创建的,而不是在另一个 war 或 jar 中创建的。 Glassfish 3.1 服务器。
请帮忙。
谢谢。
i have created a stateless session bean like this :
@WebServlet(name = "ProductController", urlPatterns = {"/ProductController"})
public class ProductController extends HttpServlet {
@EJB
private ProductFacadeBean productBean;
}
@Stateless
public class ProductFacadeBean extends AbstractFacade<Product> implements ProductFacadeLocalInterface {
@PersistenceContext(unitName = "OnlineStorePU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public ProductFacadeBean() {
super(Product.class);
}
}
@Local
public interface ProductFacadeLocalInterface {
void create(Product product);
void edit(Product product);
void remove(Product product);
Product find(Object id);
List<Product> findAll();
List<Product> findRange(int[] range);
int count();
}
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
Question :
What is the error about ? How to
solve it ?javax.naming.NamingException: Lookup
failed for
'java:comp/env/Controller.ProductController/productBean'
in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming}
[Root exception is
javax.naming.NamingException:
Exception resolving Ejb for 'Remote
ejb-ref
name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session'
. Actual (possibly internal) Remote
JNDI name used for lookup is
'EJB.ProductFacadeBean#EJB.ProductFacadeBean'
[Root exception is
javax.naming.NamingException: Lookup
failed for
'EJB.ProductFacadeBean#EJB.ProductFacadeBean'
in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming}
[Root exception is
javax.naming.NameNotFoundException:
EJB.ProductFacadeBean#EJB.ProductFacadeBean
not found]]]
The full exception is attached here :
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1392: Error instantiating servlet class Controller.ProductController
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class Controller.ProductController
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session into class Controller.ProductController
root cause
javax.naming.NamingException: Lookup failed for 'java:comp/env/Controller.ProductController/productBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]]
root cause
javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=Controller.ProductController/productBean,Remote
3.x interface =EJB.ProductFacadeBean,ejb-link=null,lookup=,mappedName=,jndi-name=EJB.ProductFacadeBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' [Root exception is javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]]
root cause
javax.naming.NamingException: Lookup failed for 'EJB.ProductFacadeBean#EJB.ProductFacadeBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found]
root cause
javax.naming.NameNotFoundException: EJB.ProductFacadeBean#EJB.ProductFacadeBean not found
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1 logs. GlassFish Server Open Source Edition 3.1
- When i created the session for
entity bean using netbeans it
created the abstract facade for us ?
What is the purpose ? I know facade
pattern is to serve as interface to
route the request ? - What is the use of ejb-jar.xml ?
All settings are default.
The ejb bean is created inside java ee 6 web application rather than in another war or jar.
Glassfish 3.1 server.
Please help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要通过引用 bean 的接口而不是直接引用其实现来注入 bean。像这样......
然后您可以使用在
ProductFacadeLocalInterface
中公开的接口方法来访问 bean。You need to inject the bean by refering to its interface and not to its implementation directly. Something like this ..
You can then access the bean using the interface methods that you exposed in
ProductFacadeLocalInterface
.您必须使用该界面。如果您使用 Seam Solder 和 CDI,您可以使用以下命令指定确切的实现
you must use the interface. If you use
Seam Solder
andCDI
you can specify the exact implementation with