2016-12-24 7 views
0

私は春とjpaを初めて使っています。私は似たような話題を探していて、まだ私の問題を解決することはできません。テストファイルでEmployeeRepository(Imp)をオートワイヤリングしようとしましたが、常にnullを返します...すべてのコードは同じパッケージの下にあります。あなたのお時間をありがとうございました。spring @Autowiredリポジトリがnullを返す

もう一つの問題は、それは私が

@Autowired 
    private EmployeeRepositoryImp er; 

以下

@Autowired 
    private EmployeeRepository er; 

私のコードですが...(私は運の両方を試してみました)を使用すべきです

package com.rw.examples.hibernate_ogm_neo4j; 

    import javax.persistence.EntityManager; 
    import javax.persistence.EntityManagerFactory; 
    import javax.persistence.Persistence; 

    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Configuration; 

    @Configuration 
    public class AppConfig { 

     @Bean 
     public EntityManager entityManager() { 
      return entityManagerFactory().createEntityManager(); 
     } 

     @Bean 
     public EntityManagerFactory entityManagerFactory() { 
      return Persistence.createEntityManagerFactory("ogm-neo4j"); 
     } 

    } 



    package com.rw.examples.hibernate_ogm_neo4j; 

    import org.springframework.data.jpa.repository.JpaRepository; 
    import org.springframework.stereotype.Repository; 

    @Repository 
    public interface EmployeeRepository extends JpaRepository<Employee, Long>{ 

    } 




    package com.rw.examples.hibernate_ogm_neo4j; 

    import javax.persistence.EntityManager; 

    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.data.jpa.repository.support.SimpleJpaRepository; 
    import org.springframework.stereotype.Repository; 

    @Repository 
    public class EmployeeRepositoryImp extends SimpleJpaRepository<Employee, Long> implements EmployeeRepository{ 

     private EntityManager entityManager; 

     @Autowired 
     public EmployeeRepositoryImp(Class<Employee> domainClass, EntityManager em) { 
      super(domainClass, em); 
      // TODO Auto-generated constructor stub 
      this.entityManager = em; 
     } 
    } 



    package com.rw.examples.hibernate_ogm_neo4j; 

    import javax.persistence.EntityManager; 
    import javax.persistence.EntityManagerFactory; 

    import org.junit.Test; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
    import org.springframework.context.annotation.ComponentScan; 
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
    import org.springframework.stereotype.Controller; 

    @Controller 
    @EnableJpaRepositories (basePackages = {"com.rw.examples.hibernate_ogm_neo4j"}) 
    @ComponentScan(basePackages = {"com.rw.examples.hibernate_ogm_neo4j"}) 
    public class RepositoryTest { 


    @Autowired 
    private EmployeeRepositoryImp er; 

    @Test 
    public void testRepository() { 

     ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 

     EntityManagerFactory emf = ctx.getBean(EntityManagerFactory.class); 
     EntityManager em = emf.createEntityManager(); 


     System.out.println("testRepository"); 
     er.save(new Employee("Frank")); 
     System.out.println("list employees using repository"); 
     Iterable<Employee> employees = er.findAll(); 
     employees.forEach(e->System.out.println(e.toString())); 
    } 
} 

答えて

0

が最終的にこの問題を解決しました。 JUNITテストを実行するには、@ RunWithを使用する必要があります。

@Configuration 
@EnableJpaRepositories 
public class AppConfig {...} 

最後に、jparepositoryとうまく動作しない休止OGMように思える:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes=AppConfig.class, loader = AnnotationConfigContextLoader.class) 
public class RepositoryTest {...} 

もAppConfig.javaで

には、以下の注釈を持っている必要があります。まだ勉強中です...

+0

あなたがSpringを使用しているなら、[Spring Data Neo4j](https://github.com/spring-projects/spring-data-neo4j)を試してみることをお勧めします。これは、Neo4jとSpringの背後にある開発者の両方に支えられています。 – digx1

0

EmployeeRepositoryの具体的な実装は必要ありません。そのクラスをコメントアウトし、EmployeeRepositoryをオートワイヤします。

また、テストで@Controller注釈は必要ありません。

+0

ありがとうございます。私はあなたの提案を試みましたが、それはまだ動作していません... –

+0

春はすべてのパッケージまたはネストされた依存性が失敗しているコンポーネントスキャンから多くの理由があります依存性注入を行うことができないのでnullです。 mavenベースのディレクトリ構造を使用し、SpringBootTestを使用してリポジトリをテストすることをお勧めします。 –