【发布时间】:2016-03-03 08:25:14
【问题描述】:
我正在尝试将 Spring JPA 与 MongoDB 集成。我的意图是从 mongo DB 中检索数据。注入我的存储库时出现以下错误。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.org.coop.society.data.mongo.repositories.MaterialMasterRepository com.org.coop.security.service.MongoService.materialMasterRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'materialMasterRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
下面给出我的配置sn-p。
TestMongoDBConfig.java
@Configuration
@EnableMongoRepositories(basePackages = {"com.abc.data.mongo.repositories"})
@ComponentScan(basePackages = "com.abc")
@PropertySource("classpath:applicationTest.properties")
public class TestMongoDBConfig extends AbstractMongoConfiguration {
@Autowired
private Environment env;
@Override
protected String getDatabaseName() {
return "retail";
}
@Override
@Bean
public MongoClient mongo() throws Exception {
MongoClient client = new MongoClient("localhost", 27017);
return client;
}
@Override
protected String getMappingBasePackage() {
return "com.abc.data.mongo.entities";
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(), getDatabaseName());
}
}
MaterialMaster.java 在 com.abc.data.mongo.entities 包中
@Document(collection = "materialMaster")
public class MaterialMaster {
@Id
private Long materialId;
@Field
private String name;
MaterialMasterRepository.java 在 com.abc.data.mongo.repositories 包中
public interface MaterialMasterRepository extends MongoRepository<MaterialMaster, Long> {
}
MongoService.java 在 com.abc.service 包中
@Service
public class MongoService {
@Autowired
private MaterialMasterRepository materialMasterRepository;
public void getMaterials() {
List<MaterialMaster> materials = materialMasterRepository.findAll();
System.out.println(materials);
}
}
Junit 类如下所示
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan(basePackages = "com.abc")
@ContextHierarchy({
@ContextConfiguration(classes={TestMongoDBConfig.class})
})
public class ModuleWSTest {
@Autowired
private MongoService mongoService;
@Test
public void testModule() {
mongoService.getMaterials();
}
}
我已经尝试了所有可能的更改(据我所知),但没有运气。非常感谢任何帮助。
【问题讨论】: