Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SpringBoot can't find bean to inject

I have a simple SpringBoot app which has following classes:

org.domain.abcpoc.controllers.DataController.java

package org.domain.abcpoc.controllers;

import org.domain.abcpoc.entities.UserEntity;
import org.domain.abcpoc.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/data")
public class DataController {
    @Autowired
    private UserRepository repo;

    @GetMapping()
    public String test() {
        return "test";
    }

    @GetMapping("/user")
    public UserEntity getUser() {
        return repo.findById("E7####J2####1##").orElse(null);
    }
}

org.domain.abcpoc.repositories.UserRepository.java

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package org.domain.abcpoc.repositories;

import org.domain.abcpoc.entities.UserEntity;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<UserEntity, String> {
}

My main Spring class is:

package org.domain.abcpoc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

And the exception I get is:

APPLICATION FAILED TO START


Description:

Field repo in org.domain.abcpoc.controllers.DataController required
a bean of type ‘org.domain.abcpoc.repositories.UserRepository’ 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
‘org.domain.abcpoc.repositories.UserRepository’ in your
configuration.

Could something else be the issue based on this exception and provided classes?
Database configuration?
pom.xml?

This is part of a Java module, could there be something in the parent pom.xml?

>Solution :

There’s one annotation missing right in the main Spring class. Try adding @EnableJpaRepositories as:

@SpringBootApplication
@EnableJpaRepositories
public class ClmConnectorService {
    public static void main(String[] args) {
        SpringApplication.run(ClmConnectorService.class, args);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading