我是spring和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()));
}
}
如果你使用Spring,那麼我建議你試試[Spring Data Neo4j](https://github.com/spring-projects/spring-data-neo4j)。它受到Neo4j和Spring背後的開發者的支持。 – digx1