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

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Boot application-The bean could not be registered

Spring Boot application failed to start, bean could not be found

Spring Boot Bean could not be found

Spring Boot 2.1 EntityManagerFactory Bean could not be found

could not found bean for MongoRepository (Spring Boot)

Spring-boot:bean could not be found

spring boot could not find bean of type

Spring boot basic application: field NotesRepository required a bean of type 'com.demo.NotesRepository' that could not be found

Registered RendezvousChannel bean cannot be found within Spring Application Context

Spring application context reloading doesn't update bean registered by DelegatingFilterProxy

Repository bean defined in @EnableJpaRepositories declared on Application, could not be registered. A bean with that name has already been defined

creating bean of a class in Spring Boot application?

Bean validation fails for Spring Boot application

No Bean Found in Spring Boot application context

spring boot application not working bean error

Could not autowire field:RestTemplate in Spring boot application

JUnit Spring Boot Application - Could not autowire fields

Spring boot Field required a bean of type that could not be found

Bean parameter could not be found when migrating spring boot

The bean 'metaDataSourceAdvisor', defined in null, could not be registered

Custom LineMapper could not be registered from xml configuration when using spring batch and spring boot 2.2.5

How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

How to make bean injection in spring boot mvc web application

How to correctly initialise a Bean in the main method of a Spring Boot application?

How to inject RequestScope bean in spring boot wicket application

Every bean/class instance created twice in spring boot application

How to fix 'Error Creating Bean' (BeanCreationException) in Maven Spring Boot Application?

Spring Boot Application can not inject Bean from another module

How to autowire a bean in other class in a Spring Boot application?