Spring Data Jpa 获取错误:“com.example.demo.restController中的字段tdlRepo需要一个类型为'com.example.demo.tdlRepo'的bean,但找不到,”

o7jaxewo  于 7个月前  发布在  Spring
关注(0)|答案(2)|浏览(84)

这是我得到bug的文件

@RestController
public class restController {
    @Autowired
    tdlRepo tdlRepo;
    @GetMapping("Hello/{name}")
    public String getName(@PathVariable String name){
        return "Hello" + name;
    }

    @GetMapping("GetData")
    public List<ListtodoEntity> getData(){
        return tdlRepo.findAll();
    }
}

字符串
已完成的错误消息:com.example.demo.restController中的字段tdlRepo需要类型为“com.example.demo.tdlRepo”的Bean,但找不到。
注入点有以下注解:- @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在配置中定义一个类型为“com.example.demo.tdlRepo”的bean。
我尝试了以下方法:
1.在application.properties中配置数据源
1.在application.properties中配置JPA
1.检查依赖性
1.检查注解但不工作。帮助我

fbcarpbf

fbcarpbf1#

我认为主要的问题是主类在项目中的位置,所以从我在你的类中看到的,你的主类应该位于

com.example

字符串

com.example.demo.something


原因是Spring将扫描从main类当前位置开始的所有子包,因此如果它在com.example.demo中,它将扫描所有com.example.demo, com.example.service, com.example.utility的子包
如果你不想移动你的主类,你可以简单地添加额外的属性到你的@SpringBootApplication中,你将提到你的bean被定义的所有包https://www.baeldung.com/spring-component-scanning

@SpringBootApplication(scanBasePackages={"com.example.demo","com.example.service","com.example.utility"})

  • 但我建议你简单地将你的主类移到包的上方。*

接下来,您需要在application.properties中输入3行

#
# JDBC properties
#
spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=root
spring.datasource.password=rootpassword


最后一件事是pom. xml中的依赖项列表

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


另外,构造函数注入比构造函数中的字段注入更好

@RestController
public class restController {

    TdlRepo tdlRepo;

    @Autowired
    restController(TdlRepo theTdlRepo) {
        tdlRepo=theTdlRepo;
    }

    @GetMapping("Hello/{name}")
    public String getName(@PathVariable String name){
        return "Hello" + name;
    }

    @GetMapping("GetData")
    public List<ListtodoEntity> getData(){
        return tdlRepo.findAll();
    }
}


请记住,我用大写的T命名了repository interface。

public interface TdlRepo extends JpaRepository<ListtodoEntity, Long>{}


(再次大写T
完成所有这些更改后,请确保使用

Maven > Reload ( IntelliJ )

Maven > Update Project > Force Update of Snapshots/Releases > Ok ( Eclipse )

e7arh2l6

e7arh2l62#

在Java中,类,接口和枚举都应该用Pascal大小写编写。
Spring使用类名命名bean,并将第一个字母转换为。

有关Spring中Beans命名约定的更多信息,您可以在此处阅读:Bean-Name

相关问题