【发布时间】: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
appcontextinitialized? -
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