2013-10-31 28 views
3

我試圖讓Spring數據JPA與EJB和CDI(Java EE 7)一起工作。 那麼,我遵循文檔(http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpd.misc.cdi-integration),但仍然無法@注入我的存儲庫在無狀態的ejb。 這裏是代碼:無法在Java EE 7堆棧上獲得Spring Data JPA + CDI

@Configuration 
@EnableJpaRepositories 
public class EntityManagerFactoryProducer { 

@Produces 
@ApplicationScoped 
public EntityManagerFactory entityManagerFactory() { 
    return Persistence.createEntityManagerFactory("mypu"); 
} 

public void close(@Disposes EntityManagerFactory entityManagerFactory) { 
    entityManagerFactory.close(); 
} 
} 

$

public interface TipoFolhaRepository extends JpaRepository<TipoFolha, Long> { 

List<TipoFolha> findByNome(String nome); 

TipoFolha findByTipo(String tipo); 
} 

$

@Stateless 
public class TipoFolhaFacade extends AbstractFacade<TipoFolha> { 

@Inject 
TipoFolhaRepository tpRepo; 

@Override 
public List<TipoFolha> findAll(){ 
    return tpRepo.findAll(); 
} 
} 

跟隨誤差。 WELD-001408類型[TipoFolhaRepository]帶有限定符[@Default]在注入點[[BackedAnnotatedField] @Inject com.mycompany.ejb.TipoFolhaFacade.tpRepo]的不滿意依存關係

我失蹤了嗎? = S

+0

嗨發現,你設法找到一個解決方案?你可以發佈嗎? – ieugen

回答

2

您需要在存儲庫類所在的模塊中啓用CDI bean-discovery-mode="all"。這意味着創建的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" 
     bean-discovery-mode="all"> 
</beans> 

不同的發現模式的簡短解釋可在this Oracle blogpost