【问题标题】:Interface not recognized as a Bean by Spring Boot接口未被 Spring Boot 识别为 Bean
【发布时间】:2022-01-03 12:35:17
【问题描述】:

好的,在否决票出现之前,这个问题与以下内容有何不同: Repository Injection not been recognized as bean: 注释主类以使其扫描基础包对我不起作用。

'Field required a bean of type that could not be found.' error spring restful API using mongodb:嗯,我没有使用 MongoDB,但即使我使用过,该问题的答案也不能解决我的问题。我的包设置正确,我尝试了@Service注解,确保IntelliJ导入了正确的Service,尝试用@Component注解控制器,确保没有2个同名的bean,重命名了Bean Spring拒绝看看,尝试清除 IntelliJ 的缓存,重新启动我的机器,没有。

D:.
│   .classpath
│   .project
│   DiscordConfApplication.java
│
├───bin
├───controller
│       Controller.java
│       DiscordUserController.java
│
├───entity
│       DiscordEntity.java
│       DiscordUser.java
│       Guild.java
│       Member.java
│       Permissions.java
│       Role.java
│       Settings.java
│
└───service
        DiscordUserRepo1.java
        DiscordUserService.java

这是我项目的结构。 com.example.discordconf 定义为包,它包含bincontrollerentityservice

当我尝试运行我的 Spring 应用程序时出现此错误:

APPLICATION FAILED TO START
***************************

Description:

Field discordUserRepo1 in com.example.discordconf.service.DiscordUserService required a bean of type 'com.example.discordconf.service.DiscordUserRepo1' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.discordconf.service.DiscordUserRepo1' in your configuration.

这里是DiscordUserRepo1.java 本身:


import com.example.discordconf.entity.DiscordUser;
import org.springframework.data.jpa.repository.JpaRepository;


public interface DiscordUserRepo1 extends JpaRepository<DiscordUser, Integer> {
    //Optional<DiscordUser> findById(Integer integer);
}

DiscordUserService.java


import com.example.discordconf.entity.DiscordUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DiscordUserService {
    @Autowired
    DiscordUserRepo1 discordUserRepo1;

    public void addNewDiscordUser(DiscordUser discordUser) {
        discordUserRepo1.save(discordUser);
    }

    public List<DiscordUser> getAllDiscordUsers() {
        return discordUserRepo1.findAll();
    }

    public Optional<DiscordUser> getById(int id) {
        return discordUserRepo1.findById(id);
    }


}

Controller.java


import com.example.discordconf.entity.DiscordUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.discordconf.service.DiscordUserService;



@RestController
public class Controller {
    @Autowired
    DiscordUserService discordUserService;

    @PostMapping("/adduser")
    public void addNewDiscordUSer(@RequestBody DiscordUser discordUser) {
        discordUserService.addNewDiscordUser(discordUser);
    }
}

DiscordUserController.java


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DiscordUserController {
    @GetMapping("/")
    public String home() {
        return "<h1>Welcome to Discord Conf!</h1>";
    }
}

DiscordConfApplication.java


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages="com.example.discordconf")
//@EnableJpaRepositories(basePackages = {"com.example.discordconf.Service"})
public class DiscordConfApplication {

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

}

我该怎么办?这几天我一直在尝试解决这个问题,这让我想把头发扯下来。

【问题讨论】:

    标签: java spring-boot spring-mvc


    【解决方案1】:

    我认为您需要将@Repository 添加到 DiscordUserRepo1 类

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      相关资源
      最近更新 更多