steve
steve

Reputation: 113

SpringBoot JPA @Autowired no bean found exception

Edited to add proc structure:

main/java hello(package) Application(main app) TestRestController models(package) Test services(package) TestRepo(interface)

I'm currently looking at component scan as just released the 'repo.Test' in the exception is a clue.

I've been through numerous tutorials and questions and still cannot find the answer to my particular issue, which is most likely to be down to my lack of understanding.

I have a spring boot application that I'm adding a db to. I've been following this tutorial : https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/

However when I try and run my application (following identical steps) I get an exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testRestController': Unsatisfied dependency expressed through field 'testRepo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repo.TestRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I do have another bean autowired in, the main difference is that this bean(the one that works) has a service that implements the interface and a bean configuration class. None of the examples for JPA follow that model and it seems daft to create a service to re-implement the methods from the JPARepo.

This is the controller I'm using:

    @RestController
public class TestRestController {

    @Autowired
    GreetingService greetingService;

    @Autowired
    TestRepo testRepo;

    @RequestMapping("/hello")
    public String home() {
        return greetingService.greet();
    }

    @RequestMapping("/testrepo")
        public String testrepo() {

        Test test = new Test("steve");

        testRepo.save(test);
        Long idOftest = test.getId();

        test = null;
        test = testRepo.findById(idOftest).get();

        return "DID THIS WORK::::: + "+ test.toString();
    }

with the interface being

    @Repository
public interface TestRepo extends JpaRepository<Test, Long> {
}

and the model:

    @Entity
@Data
public class Test {

    private final String name;

    public Test(String name){
        this.name = name;
    }

    @Id
    @GeneratedValue
    private Long id;

    public Long getId(){
        return this.id;
    }
}

The application main is:

    @SpringBootApplication
public class Application {

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

//    @Bean
//    public CommandLineRunner commandLineRunner(ApplicationContext ctx ) {
//        return args -> {
//
//            System.out.println("Let's inspect the beans provided by Spring Boot:");
//
//            String[] beanNames = ctx.getBeanDefinitionNames();
//            Arrays.sort(beanNames);
//            for (String beanName : beanNames) {
//                System.out.println(beanName);
//            }
//
//
//        };
//    }


}

I recently commented out the bean annotation to see if that was causing an issue.

Thank you for any help in advance!

Upvotes: 0

Views: 1091

Answers (1)

Abhijeet
Abhijeet

Reputation: 4309

You are getting this exception because your class TestRepo is part of repo package, which is not the sub-package hierarchy of Application class package. @SpringBootApplication defines an automatic component scan on the packages which are subpackages of your main class i.e Application. If you want to resolve this issue without changing your package hierarchy add below line with @SpringBootApplication:

@ComponentScan({"repo","your.application.class.package"}) //// add the names of the packages where the controllers, services, repositories beans are stored

Upvotes: 0

Related Questions