【发布时间】:2014-11-04 19:29:04
【问题描述】:
每当我在企业应用程序项目中使用 CDI 托管 bean 时,都会收到 CDI 部署失败:WELD-001408 异常。这是我得到的:
Injection method name must start with "set"
symbol: javax.persistence.PersistenceUnit
location: public javax.persistence.EntityManager de.syngenio.backend.beans.Resources.create(javax.persistence.EntityManagerFactory)
还有例外:
Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type EntityManagerFactory with qualifiers @Default
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] @Produces @ConversationScoped @PersistenceUnit public de.syngenio.backend.beans.Resources.create(EntityManagerFactory)
at de.syngenio.backend.beans.Resources.create(Resources.java:0)
这是课程:
public class Resources {
@Produces
@ConversationScoped
@PersistenceUnit(unitName = "mongodb-pu")
public EntityManager create(EntityManagerFactory emf) {
return emf.createEntityManager();
}
public void close(@Disposes EntityManager em) {
em.close();
}
}
我注入
@Inject
private EntityManager em;
我正在使用 Glassfish 4.1 和 JDK build 1.8.0_25-b17
编辑:
META-INF/beans.xml(EJB 项目):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
META-INF/persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="mongodb-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
<property name="eclipselink.nosql.property.mongo.port" value="27017"/>
<property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
<property name="eclipselink.nosql.property.mongo.db" value="webshop"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
【问题讨论】:
-
我认为您的 webapp/WEB-INF/ 文件夹中缺少 beans.xml 文件。要使 CDI 工作,即使为空,也需要此文件,例如gist.github.com/sejersbol/845053
-
其实它就在那里。对不起,我错过了写那个。我在 EJB 项目的 META-INF/ 中有一个 beans.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> </beans> -
你能把你的persistence.xml粘贴到你声明你的持久化单元的地方吗?数据源是在 gf 中创建的吗?还有其他与 db 相关的异常吗?
-
我更新了原帖。我使用 eclipselink 和 eclipselink nosql 扩展。我将扩展名放在 glassfish 模块文件夹和 mongo java 驱动程序中。实际上,如果我使用无状态会话 bean 中的 entitymanagerfactory 创建一个 entitymanager 实例,数据库连接就可以正常工作。
-
你的部署单位战争或耳朵是什么?
标签: java jakarta-ee glassfish cdi weld