Spring Boot application-The bean could not be registered

Rohan Shah :

I have just created a new Spring-boot-starter project and I am trying to use MongoRepository (Mentioning because I feel that this could be related to my problem) and I only have 4 classes that I am trying to run, like:

User.java

@Entity
public class User {

    @Column(name = "id")
    @Id
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;
}

UserController.java

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/AddUser")
    private ResponseEntity<?> getDistance(@RequestBody User user) throws Exception {
        userRepository.save(user);
        return ResponseEntity.ok(user);
    }
}

UserRepository.java

@Repository
public interface UserRepository extends MongoRepository<User, Long> {
}

Main Class

@SpringBootApplication
public class DemoApplication {

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

}

build.gradle

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()
}

Project Structure:

enter image description here

But everytime I run the code I get an exception saying:

***************************
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

I have double-checked and I am not using any annotation twice, Especially @Repository.

I have seen this question and it is still not working.

I just want to know why exactly is it saying

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

While I have only One Repository in my project

amseager :

Remove implementation 'org.springframework.boot:spring-boot-starter-data-jpa' from build.gradle

If you really need to use both types of repositories (jpa and mongo), you can play with exclusion filters for their scanning. Smth like:

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

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291259&siteId=1