I am new to spring. Previous I have used to configure the auto wiring using spring- context xml. Now my project we are using annotation directly to inject the bean.
I am getting following exception when I try to auto wire Mongo repository.
Below is the sample code.
StagingDocumentServiceImpl:
@Service
public class StagingDocumentServiceImpl implements StagingDocumentService {
private static final Logger logger = Logger.getLogger(StagingDocumentServiceImpl.class);
@Autowired
private StagingDocumentRepository stagingDocumentRepository;
/**
*
*/
@Override
@Transactional(readOnly = false)
public List<String> saveRawData(Tuple twitter) {
List<String> tweetIDs = new ArrayList<String>();
System.out.println("Twitter information get passed.." + twitter);
Status tweets = (Status) twitter
.getValueByField(TopologyConstants.TWEETSPOUT_OUT_FIELD);
//try{
CIPPayload twitterPayload = DataCaptureTwitterUtil
.getrawJSONPayloadFromTwitterResponse(tweets);
String parentID = null;
System.out.println("twitterPayload"+twitterPayload);
String tweetParentId = twitterPayload.getContent().getParentContentId();
/*if (StringUtils.isNotBlank(tweetParentId)) {
DBObject queryObject = new BasicDBObject();
queryObject.put("content.id", tweetParentId);
DBObject resultedData = stagingDocumentRepository.fi
if (resultedData == null) {
parentID = getOriginalTweet(tweetParentId);
tweetIDs.add(parentID);
}
}*/
Test test = stagingDocumentRepository.save(twitterPayload);
System.out.println("test-->"+test);
return tweetIDs;
}
StagingDocumentRepository:
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {
}
The problem is I do know how to configure using @bean annotation.
I have tried to configure like below.
After all below answer I have changed the configuartion as below.
But changing this also does not work.
@Configuration
public class AppConfig {
@Bean
@Bean
public StagingDocumentRepository stagingDocumentRepository(){
return new StagingDocumentRepository() {
@Override
public <S extends CIPPayload> S save(S entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public CIPPayload findOne(Serializable id) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterable<CIPPayload> findAll(Iterable<Serializable> ids) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean exists(Serializable id) {
// TODO Auto-generated method stub
return false;
}
@Override
public void deleteAll() {
// TODO Auto-generated method stub
}
@Override
public void delete(Iterable<? extends CIPPayload> entities) {
// TODO Auto-generated method stub
}
@Override
public void delete(CIPPayload entity) {
// TODO Auto-generated method stub
}
@Override
public void delete(Serializable id) {
// TODO Auto-generated method stub
}
@Override
public long count() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Page<CIPPayload> findAll(Pageable pageable) {
// TODO Auto-generated method stub
return null;
}
@Override
public <S extends CIPPayload> List<S> save(Iterable<S> entites) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<CIPPayload> findAll(Sort sort) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<CIPPayload> findAll() {
// TODO Auto-generated method stub
return null;
}
};
}
Output:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stagingDocumentServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository com.biomorf.cip.datacapture.processing.serviceImpl.StagingDocumentServiceImpl.stagingDocumentRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) [spring-boot-1.1.4.RELEASE.jar:1.1.4.RELEASE]
at com.biomorf.cip.datacapture.Application.main(Application.java:32) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository com.biomorf.cip.datacapture.processing.serviceImpl.StagingDocumentServiceImpl.stagingDocumentRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 15 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
should be like this:
@Respository
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {
}
and then :
@Configuration
@EnableMongoRepositories
public class ThisIsAnAppConfigClass{
@Bean
public StagingDocumentRepository stagingDocumentRepository(){
return new StagingDocumentRepository();
}
}
try defining this in your spring config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean class="path.to.your.stagingdocumentrepository" id="StagingDocumentRepository" />
</beans>
equivalent without xml is like this:
@Configuration
public class ThisIsAnAppConfigClass{
@Bean
public StagingDocumentRepository stagingDocumentRepository(){
return new StagingDocumentRepository();
}
}
The error message
No qualifying bean of type [com.biomorf.cip.datacapture.repository.payload.StagingDocumentRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
tells you that spring found no StagingDocumentRepository
bean that can be injected in your StagingDocumentServiceImpl
.
You should make sure you define a StagingDocumentRepository
bean. To do so you can use various ways.
StagingDocumentRepository
with a scanned annotation (eg @Repository
).Java config:
@Bean
public StagingDocumentRepository stagingDocumentRepository() {
return new StagingDocumentRepository(..);
}
Your problem is that it returns a null value.
@Bean
@Qualifier("stagingDocumentRepository")
public StagingDocumentRepository stagingDocumentRepository() {
return new StagingDocumentRepository(..);
}
尝试使用 @repository 或 @EnableMongoRepositories
You need to understand that when using Spring Data, the implementation of the repository specified is provided by Spring Data. You should not provide any such implementation.
In your case the implementation of StagingDocumentRepository
would be provided by Spring Data if you have configured Spring Data correctly.
The fact that Spring reports that no bean of type StagingDocumentRepository
exists, means that you have not setup Spring Data Mongo correctly.
Your Java config would look like this:
@Configuration
@EnableMongoRepositories("package.under.which.repositories.reside")
public class YourConfig {
//whatever beans you need
}
Your repository does not need to contain any extra annotation to be picked up by Spring Data. It would just be:
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.