spring 通过上下文检索存储库Bean时返回“没有可用的合格Bean

fumotvh3  于 2022-12-02  发布在  Spring
关注(0)|答案(1)|浏览(157)

我有一个非常简单的Springboot JPA项目,由一个主类、一个仓库和一个实体组成。首先,我遇到了一个问题,Springboot在启动时抛出了一个错误,但这个问题已经解决了。现在,我面临着无法在我的主类中检索仓库Bean的问题。
它抛出 “没有可用的”com.d043114.minimalJPA.CityRepository“类型的合格Bean”
存储库如下所示

package com.d043114.minimalJPA;

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

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

}

实体是这样的:

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;

}

主类也是基本的

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 );

    }

}
c3frrgcw

c3frrgcw1#

问题出在这一行

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

我把它换成了

@EnableJpaRepositories("com.d043114.minimalJPA")

并删除了这一行

@EntityScan("com.d043114.minimlaJPA")

它正在工作。
此外,如果删除所有这些线,那么它也是工作。

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

因此,最终代码为CityRepository.java

package com.d043114.minimalJPA;

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

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

}

City.java

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;

}

主类MinimalJpaApplication.java

@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 ");
        }
    }

}

如果您展开@SpringBootApplication,您将看到它已经处理了所有这些事情。
关于@EntityScan和@ComponentScan,请参见以下链接https://www.baeldung.com/spring-entityscan-vs-componentscan
“我们应该知道,使用@EntityScan将禁用对实体的Sping Boot 自动配置扫描。”
添加pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.d043114</groupId>
    <artifactId>minimalJPA </artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>minimalJPA </name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

相关问题