spring “无法自动连接,找不到类型为...的Bean”,带有Simple项目

f45qwnt8  于 2023-03-07  发布在  Spring
关注(0)|答案(5)|浏览(365)

我下载了简单的JPA Spring Boot tutorial,它运行得很好。然而,当我试图在我自己的测试项目中复制这个简单的行为时,我在返回CommandLineRunner的Application.demo()方法中的bean注入上得到了一个“could not autowire”错误。这个项目太简单了,我甚至不知道该提交什么,但下面是POM:

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.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-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

以及申请表。

package com.example;

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Bean
    //errors with: "Could not autowire. No beans of 'SimpRepository' type found"
    public CommandLineRunner demo(SimpRepository repository) {
        return (args) -> {

        };
    }
}

和存储库服务:

package com.example;
public interface SimpRepository extends CrudRepository<Simp, Long> {

}

适用于以下实体:

package com.example;
@Entity
public class Simp {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String value;

    public Simp(String value) {
        this.value = value;
    }
}
mefy6pfw

mefy6pfw1#

有几种可能。
您需要将@EnableJpaRepositories(basePackages = {"your.pkg.here"})添加到TestApplication中,这将告诉Spring Data在指定的包下查找您的存储库类,如果存储库包名称与TestApplication相同,则可以跳过basePackages部分。
类似地,如果您的TestApplicationSimpRepository不在同一个包中,则需要添加一个@ComponentScan,其中包含所有相关包的列表。

qcuzuvrc

qcuzuvrc2#

我错过了一个简单的类@Component。这可能是一个基本问题。

agxfikkp

agxfikkp3#

在存储库类上面添加一个简单的@Repository注解,它就可以正常工作。

zbsbpyhn

zbsbpyhn4#

我有一个类似的问题IDEA-271551刚刚打开与JetBrains。请投赞成票。
在我的例子中,我通过用**@Repository**构造型显式地注解我的存储库接口来解决(不是实际的解决,而是变通方法)。
但是,我还注意到,如果以后再次删除注解,IDE不再抱怨。
很奇怪,但我的情况就是这样。

shyt4zoc

shyt4zoc5#

我忘记<-libs.mapstructprocessor->在build.gradle的依赖项中添加“annotationProcessor〈〈〉〉“。添加时已修复。

dependencies {
    
    implementation  libs.mapstruct
    implementation  libs.mapstructprocessor
    implementation  libs.lombok

    annotationProcessor libs.lombok
    annotationProcessor libs.mapstructprocessor
}

相关问题