春季引导应用程序 – 无法注册该Bean

huangapple 未分类评论48阅读模式
标题翻译

Spring Boot application-The bean could not be registered

问题

我刚刚创建了一个新的Spring Boot Starter项目,我正在尝试使用MongoRepository(提到这个是因为我觉得这可能与我的问题有关),我只有4个类我正在尝试运行,如下所示:

User.java

  1. @Entity
  2. public class User {
  3. @Column(name = "id")
  4. @Id
  5. private Long id;
  6. @Column(name = "name")
  7. private String name;
  8. @Column(name = "email")
  9. private String email;
  10. @Column(name = "password")
  11. private String password;
  12. }

UserController.java

  1. @RestController
  2. public class UserController {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @PostMapping("/AddUser")
  6. private ResponseEntity<?> getDistance(@RequestBody User user) throws Exception {
  7. userRepository.save(user);
  8. return ResponseEntity.ok(user);
  9. }
  10. }

UserRepository.java

  1. @Repository
  2. public interface UserRepository extends MongoRepository<User, Long> {
  3. }

Main Class

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(DemoApplication.class, args);
  5. }
  6. }

build.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.2.5.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.javademos'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  14. implementation 'org.springframework.boot:spring-boot-starter-web'
  15. compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.2.5.RELEASE'
  16. implementation 'com.google.maps:google-maps-services:0.1.7'
  17. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  18. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  19. }
  20. }
  21. test {
  22. useJUnitPlatform()
  23. }

项目结构:

(项目结构图片)

但是每次我运行代码时,我都会收到一个异常,上面写着:

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.
  6. Action:
  7. Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
  8. Process finished with exit code 1

我已经仔细检查过,我没有重复使用任何注解,尤其是@Repository

我已经看过了这个问题,但还是不起作用。

我只想知道为什么它会显示:

  1. The bean 'userRepository' could not be registered. A bean with that name has already been defined and overriding is disabled.
  2. While I have only One Repository in my project
英文翻译

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

  1. @Entity
  2. public class User {
  3. @Column(name = &quot;id&quot;)
  4. @Id
  5. private Long id;
  6. @Column(name = &quot;name&quot;)
  7. private String name;
  8. @Column(name = &quot;email&quot;)
  9. private String email;
  10. @Column(name = &quot;password&quot;)
  11. private String password;
  12. }

UserController.java

  1. @RestController
  2. public class UserController {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @PostMapping(&quot;/AddUser&quot;)
  6. private ResponseEntity&lt;?&gt; getDistance(@RequestBody User user) throws Exception {
  7. userRepository.save(user);
  8. return ResponseEntity.ok(user);
  9. }
  10. }

UserRepository.java

  1. @Repository
  2. public interface UserRepository extends MongoRepository&lt;User, Long&gt; {
  3. }

Main Class

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(DemoApplication.class, args);
  5. }
  6. }

build.gradle

  1. plugins {
  2. id &#39;org.springframework.boot&#39; version &#39;2.2.5.RELEASE&#39;
  3. id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
  4. id &#39;java&#39;
  5. }
  6. group = &#39;com.javademos&#39;
  7. version = &#39;0.0.1-SNAPSHOT&#39;
  8. sourceCompatibility = &#39;1.8&#39;
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation &#39;org.springframework.boot:spring-boot-starter-data-jpa&#39;
  14. implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;
  15. compile group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-data-mongodb&#39;, version: &#39;2.2.5.RELEASE&#39;
  16. implementation &#39;com.google.maps:google-maps-services:0.1.7&#39;
  17. testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
  18. exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
  19. }
  20. }
  21. test {
  22. useJUnitPlatform()
  23. }

Project Structure:

春季引导应用程序 – 无法注册该Bean

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

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. The bean &#39;userRepository&#39; could not be registered. A bean with that name has already been defined and overriding is disabled.
  6. Action:
  7. Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
  8. 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

  1. The bean &#39;userRepository&#39; 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

答案1

得分: 11

build.gradle中移除implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

如果你确实需要同时使用两种类型的存储库(jpa和mongo),你可以通过排除过滤器来调整它们的扫描。类似于:

  1. @EnableMongoRepositories(basePackageClasses = UserRepository.class)
  2. @EnableJpaRepositories(excludeFilters =
  3. @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserRepository.class))
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }
英文翻译

Remove implementation &#39;org.springframework.boot:spring-boot-starter-data-jpa&#39; 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:

  1. @EnableMongoRepositories(basePackageClasses = UserRepository.class)
  2. @EnableJpaRepositories(excludeFilters =
  3. @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserRepository.class))
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

huangapple
  • 本文由 发表于 2020年3月16日 20:28:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定