ローハン・シャー:
私はちょうど新しい春ブート・スタータープロジェクトを作成していると私は使用しようとしています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()
}
プロジェクト構造:
しかし、私は、コードを実行するたびに、私は言って例外を取得します:
***************************
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.
私は私のプロジェクトでのみ1つのリポジトリを持っていますが
amseager:
削除implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
からbuild.gradle
あなたが本当にリポジトリ(JPAとモンゴ)の両方のタイプを使用する必要がある場合は、そのスキャンに除外フィルタで遊ぶことができます。SMTHのように:
@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);
}
}