-2

OK, before the downvotes come here is how this question is different from: Repository Injection not been recognized as bean: Annotating the main class to make it scan the base package does not work for me.

'Field required a bean of type that could not be found.' error spring restful API using mongodb: well, I'm not using MongoDB, but even if I was, the answers to that question do not solve my problem. My packages are set up correctly, I tried the @Service annotation, made sure IntelliJ imported the right Service, tried annotating the controller with @Component, made sure there's no 2 beans with the same name, renamed the Bean Spring refused to see, tried clearing the cache of IntelliJ, restarted my machine, nothing.

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

This is the structure of my project. com.example.discordconf is defined as package and it contains bin, controller, entity and service.

When I try to run my Spring app I get this error:

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.

Here is DiscordUserRepo1.java itself:


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);
    }

}

What am I supposed to do? I've been trying to solve this for days and it makes me wanna tear my hair out.

1 Answers1

0

I think you need to add @Repository to the DiscordUserRepo1 class