英文:
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 = "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:
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
答案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 '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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论