我正在尝试在我的项目中实现EJB,但在部署到JBoss AS 6(最终版)时仍然遇到相同的错误。
我有两个班级:
@Stateless
@Local(ForecastReturnService.class)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ForecastReturnServiceImpl implements ForecastReturnService {
private static final Logger logger = LoggerFactory
.getLogger(ForecastReturnServiceImpl.class);
@PersistenceContext
protected EntityManager em;
}
和另一个EJB:
@Stateless
public class CacheManagementServiceImpl implements CacheManagementService {
@EJB
private ForecastReturnService forecastReturnService;
}
错误消息与我尝试注入的'forecastReturnService'bean有关:
DEPLOYMENTS IN ERROR:
Deployment "vfs:///C:/jboss/jboss-6.0.0.Final/server/default/deploy/webui.war" is in error due to the following reason(s): java.lang.RuntimeException: Could not resolve @EJB reference: [EJB Referenc
e: beanInterface 'com.ls.forecast.jpa.ForecastReturnService', beanName 'null', mappedName 'null', lookupName 'null', owning unit 'ComponentDeploymentContext@16939526{org.jboss.metadata.ejb.jboss.JBoss
EnterpriseBeanMetaData.CacheManagementServiceImpl}'] for environment entry: env/com.ls.forecast.webui.business.CacheManagementServiceImpl/forecastReturnService in unit ComponentDeploymentContext@16939
526{org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData.CacheManagementServiceImpl}
at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:1228) [:2.2.0.GA]
at org.jboss.deployers.plugins.main.MainDeployerImpl.checkComplete(MainDeployerImpl.java:905) [:2.2.0.GA]
at org.jboss.system.server.profileservice.deployers.MainDeployerPlugin.checkComplete(MainDeployerPlugin.java:87) [:6.0.0.Final]
at org.jboss.profileservice.deployment.ProfileDeployerPluginRegistry.checkAllComplete(ProfileDeployerPluginRegistry.java:107) [:0.2.2]
at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:135) [:6.0.0.Final]
at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:56) [:6.0.0.Final]
at org.jboss.bootstrap.impl.base.server.AbstractServer.startBootstraps(AbstractServer.java:827) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-5]
at org.jboss.bootstrap.impl.base.server.AbstractServer$StartServerTask.run(AbstractServer.java:417) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-5]
at java.lang.Thread.run(Thread.java:619) [:1.6.0_20]
我注意到JBoss控制台中有以下消息:
13:34:27,681 WARN [MappedReferenceMetaDataResolverDeployer] Unresolved references exist in JBossMetaData:[#CacheManagementServiceImpl:AnnotatedEJBReferenceMetaData{name=com.ls.forecast.webui.business
.CacheManagementServiceImpl/forecastReturnService,ejb-ref-type=null,link=null,ignore-dependecy=false,mapped/jndi-name=null,resolved-jndi-name=null,beanInterface=interface com.ls.forecast.jpa.ForecastR
eturnService}, #EntityForecastReturnResultServiceImpl:AnnotatedEJBReferenceMetaData{name=com.ls.forecast.webui.business.EntityForecastReturnResultServiceImpl/entityForecastReturnProcessor,ejb-ref-type
任何帮助将不胜感激!好像我在管道中遗漏了一些东西......
我想补充一点,如果我将以下内容添加到@EJB注释中,它似乎摆脱了错误:
@EJB(lookup="com.ls.forecast.jpa.ForecastReturnService/local")
private ForecastReturnService forecastReturnService;
不知道为什么我必须指定本地接口,因为我已经看过很多只有@EJB的示例/教程。为什么我需要指定本地接口?
答案 0 :(得分:2)
我为这个解决方案搜索了一个低点,终于得到了它。首先,我们需要了解标准的EE 6(便携式JNDI)JNDI。 JBoss文档很糟糕。你得寻求解决方案。
Portable JNDI(PREFIXES):
java:app
java:global
java:module
让我们看看JBoss 6.1.0 JMX控制台。
JMX控制台> service = JNDIView(在JBoss部分下)
这将为您带来JMX MBean View
现在您可以通过单击调用按钮list
行(我的最后一行)来查看JNDI树,您也可以使用listXML
但我发现这令人困惑。
在java:
部分下滚动到以global
开头的分支(因为我们希望在EAR之外远程使用EJB)。从上面的代码判断,您应该能够找到类似的路径:
全球> ForecastReturnServiceEJB(项目名称)> ForecastReturnServiceImpl!inject.ForecastReturnService
将此翻译为:
java:global/ForecastReturnServiceEJB/ForecastReturnServiceImpl!inject.ForecastReturnService
这是我们需要用于全局访问的JNDI。
@EJB(mappedName="java:global/ForecastReturnServiceEJB/ForecastReturnServiceImpl!inject.ForecastReturnService")
private ForecastReturnService forecastReturnService;
或
@EJB(mappedName="java:app/ForecastReturnServiceEJB/ForecastReturnServiceImpl!inject.ForecastReturnService")
private ForecastReturnService forecastReturnService;
还有一件事,你只能在同一个EAR中使用没有参数的@EJB
。
答案 1 :(得分:0)
我通过修改我的项目来解决这个问题,现在它被打包为耳朵而不是战争。我现在可以拥有尽可能多的ejb jar,尽管我决定借此机会重新组织项目并将所有EJB放在同一个Maven模块中。
答案 2 :(得分:0)
您实施的接口中是否有任何特定内容?
部署到JBoss AS 6的最简单的实现只有在我在本地快速尝试时才能正常工作:
package inject;
public interface ForecastReturnService {
}
要注入的bean:
package inject;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
@Stateless
@Local(ForecastReturnService.class)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ForecastReturnServiceImpl implements ForecastReturnService {
}
进行注射的bean的接口:
package inject;
public interface CacheManagementService {
}
进行注射的Bean:
package inject;
import javax.ejb.EJB;
import javax.ejb.Stateless;
@Stateless
public class CacheManagementServiceImpl implements CacheManagementService {
@EJB
private ForecastReturnService forecastReturnService;
}
日志中的相关条目:
[org.jboss.ejb3.EJBContainer] STARTED EJB: inject.ForecastReturnServiceImpl ejbName: ForecastReturnServiceImpl
[org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
ForecastReturnServiceImpl/local - EJB3.x Default Local Business Interface
ForecastReturnServiceImpl/local-inject.ForecastReturnService - EJB3.x Local Business Interface
和
STARTED EJB: inject.CacheManagementServiceImpl ejbName: CacheManagementServiceImpl
[org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
CacheManagementServiceImpl/local - EJB3.x Default Local Business Interface
CacheManagementServiceImpl/local-inject.CacheManagementService - EJB3.x Local Business Interface
两个小评论(与您的问题没有直接关系),但此处不需要@TransactionAttribute
。 REQUIRED
已经是默认值。应该在接口上使用@Local
,在类级别使用它是针对您无法更改的旧接口。