【问题标题】:Retrieving Repository Bean via Context returns "No qualifying Bean availableRetrieving Repository Bean via Context returns \"No qualifying Bean available
【发布时间】:2022-12-02 00:42:34
【问题描述】:

I have a very simple Springboot JPA Project consisting of a main class a repository and one entity. First I have had a problem the Springboot throws an error during startup, but this is solved. Now I face the problem that I cannot retrieve the Repository Bean in my main class.

It throws"No qualifying bean of type 'com.d043114.minimalJPA.CityRepository' available"

The repository looks like this

package com.d043114.minimalJPA;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CityRepository extends CrudRepository<City, Long> {

}

The entity is like this:

package com.d043114.minimalJPA;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "cities")
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO )
    private long ID;
    
    public long getID() {
        return ID;
    }

    public void setID(long iD) {
        ID = iD;
    }

    public String getName() {
        return name;
    }

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

    private String name;

}

The main class is basic as well

package com.d043114.minimalJPA;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.d043114.minimalJPA.CityRepository;

@EnableJpaRepositories("com.d043114.minimalJPA.*")
@ComponentScan(basePackages = "com.d043114.minimalJPA.*")
@SpringBootApplication
@EntityScan("com.d043114.minimlaJPA")
public class MinimalJpaApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext appcontext = SpringApplication.run(MinimalJpaApplication.class, args);
        
        
        CityRepository cityRepository =   appcontext.getBean(CityRepository.class );

    }

}

【问题讨论】:

  • Where/how is appcontext initialized?
  • Are all your beans in the same package?
  • There was a cut&Paste Error. appcontext get initlized from the SpringApplication run. Everything is under one package.
  • @Habi , please remove @EnableJpaRepositories("com.d043114.minimalJPA.*") , @EntityScan("com.d043114.minimlaJPA") , @ComponentScan(basePackages = "com.d043114.minimalJPA.*") . It will work . I tried same at my end and its working.

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

The issue is with this line

@EnableJpaRepositories("com.d043114.minimalJPA.*")

I replaced this with

@EnableJpaRepositories("com.d043114.minimalJPA")

and removed this line

@EntityScan("com.d043114.minimlaJPA")

It is working .

Also if remove all together these lines then also it is working .

@EnableJpaRepositories("com.d043114.minimalJPA.*")
@ComponentScan(basePackages = "com.d043114.minimalJPA.*")

So final code is

@SpringBootApplication
public class MinimalJpaApplication
{

    public static void main(String[] args)
    {
        ConfigurableApplicationContext appcontext = SpringApplication.run(MinimalJpaApplication.class, args);
        CityRepository cityRepository =  appcontext.getBean(CityRepository.class );


        if ( cityRepository != null )
        {
            System.out.println("We are goood ");
        }
    }

}

If you expand @SpringBootApplication , you will see its already taking care of all these things .

Regarding @EntityScan and @ComponentScan , please see below link https://www.baeldung.com/spring-entityscan-vs-componentscan

"We should be aware that using @EntityScan will disable Spring Boot auto-configuration scanning for entities."

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2016-11-07
    • 1970-01-01
    • 2022-10-14
    • 2018-01-02
    • 2011-11-05
    相关资源
    最近更新 更多