Search code examples
javahibernatespring-bootrepositorycomponent-scan

org.sf.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type X available


I'm trying to setup a simple project with a user repository in Spring Boot with Hibernate as JPA, I tried this and this tutorials and in both (and everywhere) @SpringBootApplication seems to be sufficient to make the repository reachable throughout the app's scope since it includes @ComponentScan, except, in my case, it is not. Even this answer looks tailored to my situation, but no.

Project structure

project
│   └── src
│       └── main
│           └── java
│               └── medisam
|                    └── Application.java
│   └── src
│       └── main
│           └── java
│               └── medisam
│                   └── entity
│                       └── User.java
|                       └── UserRepository.java
│    └── src
│        └── main
│            └── java
│               └── medisam
│                   └── controller
│                       └── MainController.java

With such structure and this code:

@SpringBootApplication
public class Application {
    ...
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);
    @Autowired
    private UserRepository repo;

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

I get:

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

It is only when I do this:

@ComponentScan(basePackages = {"medisam.controller", "medisam.entity"})
@EntityScan({"medisam.entity"})
@EnableJpaRepositories(basePackages = {"medisam.entity"})
public class Application {
    ...
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);
    @Autowired
    private UserRepository repo;

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

that I get a working code but, if there is really a way of achieving this with only @SpringBootApplication, I'd like to use it.

Could it be that @SpringBootApplication is not scanning correctly?

Help please

EDIT

UserRepository.java

package medisam.entity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

}

User.java

package medisam.entity;

import java.util.Set;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private String email;
    private String username;
    private String password;
    private boolean enabled;

    @ManyToMany(targetEntity = Role.class)
    @Access(AccessType.FIELD)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private Set<Role> roles;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

Solution

  • I reviewed your code on gihub and found you have imported wrong package name in class Application.java and ServiceResponse.java.

    Please import main package as package medisam; instead of package hello;.

    like below:

    Your Application class:

    package medisam;
    import medisam.entity.UserRepository;
    import nz.net.ultraq.thymeleaf.LayoutDialect;
    import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Description;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
    import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
    import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
    import org.thymeleaf.spring5.SpringTemplateEngine;
    import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
    import org.thymeleaf.spring5.view.ThymeleafViewResolver;
    import org.thymeleaf.templatemode.TemplateMode;
    import org.thymeleaf.templateresolver.ITemplateResolver;
    import org.thymeleaf.templateresolver.UrlTemplateResolver;
    
    @SpringBootApplication
    public class Application {
    

    and here is your ServiceResponse

    package medisam;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import org.apache.catalina.util.ResourceSet;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class ServiceResponse {