Java 无法注册Spring启动应用程序bean

Java 无法注册Spring启动应用程序bean,java,spring,spring-boot,Java,Spring,Spring Boot,我刚刚创建了一个新的Spring boot starter项目,我正在尝试使用MongoRepository,因为我觉得这可能与我的问题有关,我只尝试运行4个类,比如: User.java UserController.java 格雷德尔先生 项目结构: 但每次运行代码时,我都会遇到一个异常,即: *************************** APPLICATION FAILED TO START *************************** Description: T

我刚刚创建了一个新的Spring boot starter项目,我正在尝试使用MongoRepository,因为我觉得这可能与我的问题有关,我只尝试运行4个类,比如:

User.java

UserController.java

格雷德尔先生

项目结构:

但每次运行代码时,我都会遇到一个异常,即:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true


Process finished with exit code 1
我已经仔细检查过了,我没有两次使用任何注释,尤其是@Repository

我已经看到了,但它仍然不起作用

我只是想知道它到底为什么这么说

The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.
虽然我的项目中只有一个存储库

从build.gradle中删除实现“org.springframework.boot:springbootstarterdatajpa”

如果您确实需要同时使用jpa和mongo这两种类型的存储库,您可以使用排除过滤器进行扫描。Smth类似:

@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@EnableJpaRepositories(excludeFilters = 
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserRepository.class))
@SpringBootApplication
public class DemoApplication {

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

}
从build.gradle中删除实现“org.springframework.boot:springbootstarterdatajpa”

如果您确实需要同时使用jpa和mongo这两种类型的存储库,您可以使用排除过滤器进行扫描。Smth类似:

@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@EnableJpaRepositories(excludeFilters = 
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserRepository.class))
@SpringBootApplication
public class DemoApplication {

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

}

共享项目的包结构和pom.xml自从我上次使用Spring数据已经有一段时间了,但是基于从Repository接口继承的docs MongoRepository,所以我认为您不需要将UserRepository标记为@Repository,否则Spring可能会尝试注册它两次。@Rockbag在我删除它之后仍然显示相同的错误@Repository@RohanShah我的下一个最佳猜测是,SpringBootStarter数据jpa或SpringBootStarterWeb已经在内部定义了一个用户存储库,如果您使用的是高级IDE,那么您可能可以查找它。从Spring2.1.0开始,默认情况下禁用bean重写,因此如果要将bean重新声明为MongoRepository,您可以只设置spring.main.allow bean definition overriding=true。在属性spring.main.allow-bean-definition-overriding中绕过此用法的一种方法是:trueshare项目的包结构和pom.xml我上次使用的是Spring数据,但基于从Repository接口继承的docs MongoRepository,因此我认为您不需要将UserRepository标记为@Repository,否则Spring可能会尝试注册它两次。@Rockbag在我删除它之后仍然显示相同的错误@Repository@RohanShah我的下一个最佳猜测是,SpringBootStarter数据jpa或SpringBootStarterWeb已经在内部定义了一个用户存储库,如果您使用的是高级IDE,那么您可能可以查找它。自Spring 2.1.0以来,默认情况下禁用了bean重写,因此如果要将bean重新声明为MongoRepository,可以只设置Spring.main.allow bean definition overriding=true。在属性Spring.main.allow-bean-definition-overriding中绕过此用法的一种方法是:true
plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.javademos'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.2.5.RELEASE'
    implementation 'com.google.maps:google-maps-services:0.1.7'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}
***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true


Process finished with exit code 1
The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
@EnableJpaRepositories(excludeFilters = 
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserRepository.class))
@SpringBootApplication
public class DemoApplication {

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

}