gpt4 book ai didi

java - Spring Boot应用程序-bean无法注册

转载 作者:行者123 更新时间:2023-12-02 08:52:06 25 4
gpt4 key购买 nike

我刚刚创建了一个新的 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> {
}

主类

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

项目结构:

enter image description here

但是每次我运行代码时都会出现异常:

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

我见过this question但它仍然无法正常工作。

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

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

}

关于java - Spring Boot应用程序-bean无法注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60706042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com