SpringData-MongoDB:没有可用类型的限定bean

时间:2017-11-08 04:58:51

标签: java spring mongodb spring-data-mongodb

我尝试使用Spring Data构建项目以连接到MongoDB,如下所示: 的 SpringMongoConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig {

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClient("127.0.0.1"), "ReconInput");
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

        return mongoTemplate;

    }
}

ReconInputRepository.java

@Repository
public interface ReconInputRepository extends MongoRepository<ReconInput, String> {
    public List<ReconInput> findByReportingDate(String reportingDate);
}

ReconInputService.java

public interface ReconInputService {
    public List<ReconInput> getInputByReportingDate(String reportingDate);
}

ReconInputServiceImpl.java

@Service
public class ReconInputServiceImpl implements ReconInputService {

    @Autowired
    private ReconInputRepository reconInputRepository;  
    public List<ReconInput> getInputByReportingDate(String reportingDate) {
        return reconInputRepository.findByReportingDate(reportingDate);
    }   
}

App.java

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);       
        ReconInputService reconInputService = ctx.getBean(ReconInputService.class);
        List<ReconInput> inputData = reconInputService.getInputByReportingDate("2017 Nov 20");
        System.out.println(inputData.get(0).getReportId());
    }
}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ups.mongodb</groupId>
    <artifactId>MongoConnection</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MongoConnection</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>5.0.1.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.5.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

    </dependencies>
</project>

当我运行项目时,它会抛出异常:

  

没有符合条件的bean&#39; ups.mongo.service.ReconInputService&#39;   可用。

请帮我解决此错误的任何建议。谢谢!

更新1

@ComponentScan(basePackages =&#34; ups.mongo&#34;)添加到 SpringMongoConfig.java 。 然后我得到了新的问题: 创建具有名称&#39; ReconInputService的bean时出错:通过字段“reconInputRepository”表达的不满意的依赖性&#39 ;; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型&#39; ups.mongo.repository.ReconInputRepository&#39;的限定bean。可用:预计至少有1个豆有资格作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

更新2

而不是使用Spring Data MongoRepository 。我替换了从Spring数据扩展的 ReconInputRepository.java ,我自己实现如下:

ReconInputRepository.java

public interface ReconInputRepository {

    public List<ReconInput> findByReportingDate(String reportingDate);
}

ReconInputRepositoryImpl.java

@Repository
public class ReconInputRepositoryImpl implements ReconInputRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    public List<ReconInput> findByReportingDate(String reportingDate) {
        List<ReconInput> reconInputList = null;
        Query searchUserQuery = new Query(Criteria.where("reportingDate").is(reportingDate));
        reconInputList = mongoTemplate.find(searchUserQuery, ReconInput.class);
        return reconInputList;
    }
}

然后它正常工作。

我的摘要 问题可能来自Spring不支持注入界面 - 如 @amdg 建议(但是在春季启动工作 - 我不知道为什么,如果有人知道,请给我一些评论)。

参考:Spring interface injection example

更新3

最后,我找到了最简单的正确方法。 我需要做的就是将@EnableMongoRepositories({ "ups.mongo.repository" })添加到 SpringMongoConfig.java

5 个答案:

答案 0 :(得分:2)

因为你在ReconInputServiceImpl上注释了@Service,所以 请在主类

中添加ReconInputServiceImpl.class
ReconInputService reconInputService = ctx.getBean(ReconInputServiceImpl.class);

答案 1 :(得分:1)

正如我所怀疑的那样,你将普通的Spring和Spring Boot混合在一起,想要获得Spring Boot效果。

为了使用Spring Boot,您应该更新依赖项以使用Spring Boot Starters。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ups.mongodb</groupId>
    <artifactId>MongoConnection</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MongoConnection</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>    
</project>

然后只需将您的配置添加到应用根目录中(可能是ups.mongo):

@SpringBootApplication
@EnableMongoRepositories
public class App 
{
    public static void main( String[] args )
    {
        ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);       
        ReconInputService reconInputService = ctx.getBean(ReconInputService.class);
        List<ReconInput> inputData = reconInputService.getInputByReportingDate("2017 Nov 20");
        System.out.println(inputData.get(0).getReportId());
    }
}

在这种情况下,您甚至不需要SpringMongoConfig.class。而是在application.properties中添加以下配置:

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=demo

答案 2 :(得分:0)

您可以将ReconInputServiceImpl课程注释为@Service("reconInputService")

然后您可以按如下方式访问它。

ReconInputService reconInputService = ctx.getBean("reconInputService",ReconInputService.class);

答案 3 :(得分:0)

    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);       

如果使用上述构造函数,DI容器将仅从SpringMongoConfig类加载bean定义。

如果您希望DI容器以递归方式扫描特定基础包的所有bean定义,请使用此构造函数

AnnotationConfigApplicationContext(java.lang.String... basePackages)

答案 4 :(得分:0)

我以前仅添加了spring-data-mongodb软件包,这使我忙于调试“某些存储库没有合格的bean”。

添加spring-boot-starter-data-mongodb之后,一切都在spring之前完成。

application.yaml

spring:
  data:
    mongodb:
      uri: mongodb://username:password@localhost:27017
      database: test

pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
</dependency>