[英]Autowired error spring
I am new to spring.我是春天的新手。 Previous I have used to configure the auto wiring using spring- context xml.
以前我曾经使用 spring-context xml 配置自动连接。 Now my project we are using annotation directly to inject the bean.
现在我的项目我们直接使用注解来注入 bean。
I am getting following exception when I try to auto wire Mongo repository.当我尝试自动连接 Mongo 存储库时,出现以下异常。
Below is the sample code.下面是示例代码。
StagingDocumentServiceImpl: 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.问题是我知道如何使用@bean注释进行配置。
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尝试在您的 spring config.xml 文件中定义它
<?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:没有 xml 的等价物是这样的:
@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
.告诉你 spring 没有找到可以注入你的
StagingDocumentServiceImpl
StagingDocumentRepository
bean。
You should make sure you define a StagingDocumentRepository
bean.您应该确保定义了一个
StagingDocumentRepository
bean。 To do so you can use various ways.为此,您可以使用多种方法。
StagingDocumentRepository
with a scanned annotation (eg @Repository
).StagingDocumentRepository
用扫描注释(例如@Repository
)。 Java config: Java配置:
@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.您需要了解,在使用 Spring Data 时,指定的存储库的实现是由 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.在您的情况下,如果您已正确配置 Spring Data,则 Spring Data 将提供
StagingDocumentRepository
的实现。
The fact that Spring reports that no bean of type StagingDocumentRepository
exists, means that you have not setup Spring Data Mongo correctly. Spring 报告不存在类型为
StagingDocumentRepository
bean 的StagingDocumentRepository
意味着您没有正确设置 Spring Data Mongo。
Your Java config would look like this:您的 Java 配置如下所示:
@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.您的仓库并不需要包含到由Spring数据拾起任何额外的注释。 It would just be:
它只会是:
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.