【问题标题】:Spring boot, Autowire Mongo repo generates BeanCreationExceptionSpring boot,Autowire Mongo repo 生成 BeanCreationException
【发布时间】:2016-04-09 19:25:45
【问题描述】:

我阅读了几个与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

【问题讨论】:

    标签: spring mongodb maven spring-boot autowired


    【解决方案1】:

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

    重构这个

    @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>
    

    【讨论】:

    • 我不确定您为什么添加了@EnableJpaRepositories 注释。但只是添加实体扫描会给我一个新的错误,上面写着 Error Creating bean with name 'embeddedServletContainerCustomizerBeanPostProcessor'。
    • 这个 tomcat 错误你现在你的 bean 创建成功了
    • pom中没有tomcat依赖。也许我在那里遗漏了什么?!
    • 使项目清洁和建设,并重新导入 maven 依赖项
    • 我将作用域作为编译器添加到 spring-boot-starter-web 的依赖项中,但它没有帮助。仍然得到同样的错误。另外我认为如果你想有一个自定义的 ComponentScan,即使你有 SpringBootApplication 的注解,你也可以做到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2021-11-17
    • 2017-08-31
    相关资源
    最近更新 更多