Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自动接线错误弹簧_Java_Spring_Autowired_Spring Data Mongodb - Fatal编程技术网

Java 自动接线错误弹簧

Java 自动接线错误弹簧,java,spring,autowired,spring-data-mongodb,Java,Spring,Autowired,Spring Data Mongodb,我是春天的新手。我以前使用spring-context xml配置自动连接。 现在,在我的项目中,我们直接使用注释来注入bean 当我尝试自动连接Mongo存储库时,出现以下异常 下面是示例代码 StagingDocumentServiceImpl: @Service public class StagingDocumentServiceImpl implements StagingDocumentService { private static final Logger logger = L

我是春天的新手。我以前使用spring-context xml配置自动连接。 现在,在我的项目中,我们直接使用注释来注入bean

当我尝试自动连接Mongo存储库时,出现以下异常

下面是示例代码

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;
}
 public interface StagingDocumentRepository extends
  MongoRepository<CIPPayload, Serializable> {

 }
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]

尝试在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>
错误消息

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.
告诉您spring没有找到可以注入到您的
StagingDocumentServiceImpl
中的
StagingDocumentRepository
bean

您应该确保定义了一个
StagingDocumentRepository
bean。为此,您可以使用多种方法

  • 使用xml配置,如Arno_Geismar在回答中所示
  • 使用组件扫描功能,并使用扫描的注释对
    StagingDocumentRepository
    进行注释(例如
    @Repository
  • 使用Java配置来定义bean(如下所示)
Java配置:

@Bean
public StagingDocumentRepository stagingDocumentRepository() {
  return new StagingDocumentRepository(..);
}

您的问题是它返回空值

@Bean
@Qualifier("stagingDocumentRepository")
public StagingDocumentRepository stagingDocumentRepository() {
  return new StagingDocumentRepository(..);
}

尝试使用@repository或@EnableMongoRepositories

应该是这样的:

@Respository
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {

}
@Configuration
@EnableMongoRepositories("package.under.which.repositories.reside")
public class YourConfig {

    //whatever beans you need
}

您需要了解,在使用Spring数据时,指定存储库的实现是由Spring数据提供的。您不应该提供任何此类实现

在您的情况下,如果您正确配置了Spring数据,则Spring数据将提供
StagingDocumentRepository
的实现

Spring报告不存在类型为
StagingDocumentRepository
的bean,这意味着您没有正确设置Spring数据Mongo

您的Java配置如下所示:

@Respository
public interface StagingDocumentRepository extends
MongoRepository<CIPPayload, Serializable> {

}
@Configuration
@EnableMongoRepositories("package.under.which.repositories.reside")
public class YourConfig {

    //whatever beans you need
}
您的存储库不需要包含Spring数据要获取的任何额外注释。它只是:

public interface StagingDocumentRepository extends
  MongoRepository<CIPPayload, Serializable> {

}
公共接口StagingDocumentRepository扩展
蒙哥罗普托里{
}

您是否在bean配置文件中声明了
StagingDocumentRepository
?否。我知道如何执行此操作。我过去常在spring上下文文件中这样做。我知道如何使用@Bean注释进行注入。试着把它放在您的@Bean、@Qualifier(“stagingDocumentRepository”)上这就是我配置的@Bean@Qualifier(“stagingDocumentRepository”)public StagingDocumentRepository StagingDocumentRepository(){}此错误主要是由于文件夹structuring@Arno_Gesmar我已根据您的建议编辑了配置。但我还是得到了同样的例外@Repository@Arno_Gesmar对不起,每次都打扰你。我也添加了存储库,但结果仍然是same@Arno_Gesmar在进行更改之后,我无法在ThisIsAnapConfigClass类中实例化StagingDocumentRepository类型。我想我必须重写mongo存储库方法您应该有一个实现接口的具体类。然后,您的方法:public StagingDocumentRepository StagingDocumentRepository(){return new StagingDocumentRepositoryImpl();}应该返回实现
public interface StagingDocumentRepository extends
  MongoRepository<CIPPayload, Serializable> {

}