我是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