2013-10-31 25 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