2014-11-04 149 views
1

每當我在我的企業應用程序項目中使用CDI託管的bean時,我得到CDI部署失敗:WELD-001408異常。這就是我得到:CDI部署失敗:使用CDI bean時的WELD-001408(Glassfish 4.1)

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> 
+0

我認爲你在webapp/WEB-INF /文件夾中缺少beans.xml文件。 對於CDI來說,即使空如https://gist.github.com/sejersbol/845053 – rangalo 2014-11-04 19:39:10

+0

其實它在那裏。對不起,我錯過了寫這個。我在EJB項目的META-INF /中有一個beans.xml文件。 <?xml version =「1.0」encoding =「UTF-8」?> ' – mherwig 2014-11-04 19:43:08

+0

您還可以在您聲明持久性單元的位置粘貼persistence.xml嗎?數據源是用gf創建的嗎?是否還有其他與db相關的異常? – rangalo 2014-11-04 19:45:13

回答

0

使用EntityManagerFactory作爲一個參數時在託管方法中,您應該確保EntityManagerFactory也是可注射的。由於您的方法「創建」是託管的,所有參數值的創建方式與@Inject類似。我不知道爲什麼你的EntityManagerFactory不是可注射的,也不知道這是獲得它的一個實例的正確方法。我的建議是: 1.在創建方法 中以另一種方式獲得您的EntityManagerFactory 2.或者爲您的EntityManagerFactory編寫一個生產者,創建並返回一個新的EntityManagerFactory