Spring启动,Autowire Mongo repo生成BeanCreationException

时间:2016-04-09 19:25:46

标签: spring mongodb maven spring-boot autowired

我阅读了几个与autowireBeanCreationException相关的类似问题和答案,似乎问题的主要来源通常是ComponentScan注释和项目树。

但我仍然无法理解为什么我的应用程序会抛出此异常。 这是pom.xml

<?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>com.test.game</groupId>
    <artifactId>gameP</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>2.0.2-beta</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

带有SpringBootApplication注释

的应用程序运行器
package core.main.exec;

@SpringBootApplication
@Configuration
@ComponentScan("core.main")
public class ApplicationRunner implements CommandLineRunner {
    @Autowired
    GameEntityRepository repo;

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(ApplicationRunner.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Game newGame = new Game();
        newGame.setId(1213123L);
        newGame.setName("halo 123");
        newGame.setDeck("Duck");
        newGame.setMedium_url("url");
        repo.save(newGame);
    }
}

存储库定义为下面的

package core.main.controller;

public interface GameEntityRepository extends MongoRepository {
}

名为Game的Mongo实体如下所示

package core.main.mongoentity;

@Document
public class Game {
    @Id
    private Long id;
    private String deck;
    private String name;
    private String medium_url;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDeck() {
        return deck;
    }

    public void setDeck(String deck) {
        this.deck = deck;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMedium_url() {
        return medium_url;
    }

    public void setMedium_url(String medium_url) {
        this.medium_url = medium_url;
    }
}

一旦我运行应用程序,我就会收到BeanCreationException,并显示以下错误消息

  

无法自动装配字段:core.main.controller.GameEntityRepository core.main.exec.ApplicationRunner.repo

1 个答案:

答案 0 :(得分:0)

我认为这是由包扫描引起的错误

重构这个

@SpringBootApplication
@Configuration
@ComponentScan("core.main")
public class ApplicationRunner implements CommandLineRunner {}
像这样

@SpringBootApplication
@EntityScan({"core.main.mongoentity"})
@EnableJpaRepositories(basePackages = {"core.main.controller"})
public class ApplicationRunner implements CommandLineRunner {}

并且也不要使用@ComponentScan("core.main"),因为@SpringBootApplication提供了@EnableAutoConfiguration@ComponentScan

的属性

并且您的存储库也不正确您没有为MongoRepository

声明实体

重构您的存储库

public interface GameEntityRepository extends MongoRepository {}
像这样

public interface GameEntityRepository extends MongoRepository<Game,Long>{}

并在pom.xml中添加此依赖项

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
相关问题