我正在尝试创建DatastoreRepository类的bean,但是使用spring-boot 2.1.3
Description:
The bean 'bookRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
这是我的项目结构,我的应用程序主运行类位于这样的根包中
com.mycompany.project
--Application.java
--controller
--domain
--repository
带有@SpringBootApplication
的类在根包中
这是我的存储库类
import org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository;
public interface BookRepository extends DatastoreRepository<Book, Long>{
}
这是我的域名课程
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.data.annotation.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity(name = "books")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
@Id
Long id;
String title;
}
这是我的控制器类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppUserController {
@Autowired
BookRepository bookRepository;
@GetMapping("/booksave")
public String helloworld() {
bookRepository.save(new Book(3L, "author"));
return "book saved";
}
}
这是我的Application类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
答案 0 :(得分:0)
我认为问题在于您使用批注的方式,尝试将Injection更改为Constructor,例如:
@RestController
public class AppUserController {
private BookRepository bookRepository;
@Autowired
public AppUserController (
BookRepository bookRepository){
this.bookRepository= bookRepository;
}
@GetMapping("/booksave")
public String helloworld() {
bookRepository.save(new Book(3L, "author"));
return "book saved";
}
}
答案 1 :(得分:0)
将此注释@EnableDatastoreRepositories添加到您的Application.java
答案 2 :(得分:0)
如何使用Spring data rest:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
您无需编写控制器
import org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "xxxxxs", path = "xxxx")
public interface XXXXXRepository extends DatastoreRepository<XXXXX, String>
麻烦的配置!!!
@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {