我有以下设置:
EJB-JAR:
试验WAR:
问题:
但是当我尝试使用依赖集设置到jar(在MANIFEST.MF或jboss-deployment-structure.xml中)时,我总是得到Weld无法将持久性单元注入DAO的错误bean,因为它找不到具有我指定名称的持久性单元:
JBAS016069:将持久性单元注入CDI托管bean时出错。无法在部署test.war中找到名为test的持久性单元
这是一个奇怪的错误,因为在战争中容器不应该需要persistence.xml文件,因为它位于加载dao类的ejb-jar中。
那么为什么容器会抱怨缺少持久性单元,尽管它存在于jar中?
注入EntityManager(ejb-jar):
public class ShopDAO {
@PersistenceContext(unitName="test")
private EntityManager entityManager;
...
persistence.xml(ejb-jar):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="test">
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@dbms:1523:DEV11"/>
<property name="hibernate.connection.username" value="testuser"/>
<property name="hibernate.default_schema" value="TEST"/>
</properties>
</persistence-unit>
</persistence>
注射DAO(试战):
@WebServlet("/status")
public class TestServlet extends HttpServlet {
@Inject
private ShopDAO shopDAO;
protected void doGet(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
//do something with the shop dao!
}
}
错误日志:
ERROR [fail] MSC000001: Failed to start service jboss.deployment.unit."test.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."test.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named test in deployment test.war
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:110)
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:73)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:312)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:301)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:206)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:182)
at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:405)
at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:92)
at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:52)
at org.jboss.weld.injection.producer.BeanInjectionTarget.initInjector(BeanInjectionTarget.java:55)
at org.jboss.weld.injection.producer.BasicInjectionTarget.<init>(BasicInjectionTarget.java:68)
at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:49)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:126)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:88)
at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:91)
at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:71)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:264)
at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:248)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:74)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:72)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more
答案 0 :(得分:0)
我刚刚将文件 persistence.xml 从“... \ src \ main \ java \ META-INF”移动到“... \ src \ main \ webapp \ META-INF”它起作用了。如果在classes文件夹中没有META-INF / persistence.xml文件,则war不起作用。