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

huangapple go评论74阅读模式
英文:

Spring Boot application-The bean could not be registered

问题

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

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

项目结构:

(项目结构图片)

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

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

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

@Entity
public class User {

    @Column(name = &quot;id&quot;)
    @Id
    private Long id;

    @Column(name = &quot;name&quot;)
    private String name;

    @Column(name = &quot;email&quot;)
    private String email;

    @Column(name = &quot;password&quot;)
    private String password;
}

UserController.java

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping(&quot;/AddUser&quot;)
    private ResponseEntity&lt;?&gt; getDistance(@RequestBody User user) throws Exception {
        userRepository.save(user);
        return ResponseEntity.ok(user);
    }
}

UserRepository.java

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

Main Class

@SpringBootApplication
public class DemoApplication {

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

}

build.gradle

plugins {
    id &#39;org.springframework.boot&#39; version &#39;2.2.5.RELEASE&#39;
    id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
    id &#39;java&#39;
}

group = &#39;com.javademos&#39;
version = &#39;0.0.1-SNAPSHOT&#39;
sourceCompatibility = &#39;1.8&#39;

repositories {
    mavenCentral()
}

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

test {
    useJUnitPlatform()
}

Project Structure:

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

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

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

Description:

The bean &#39;userRepository&#39; 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 &#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),你可以通过排除过滤器来调整它们的扫描。类似于:

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

}
英文:

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:

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

}

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

发表评论

匿名网友

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

确定