0

I'm trying to create a bean of the DatastoreRepository class but I get the following error Iam using 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

this is my project structure I have the Application main run class in a root package like this

com.mycompany.project    
  --Application.java
  --controller
  --domain
  --repository

The class with the @SpringBootApplication is in the root package

here is my repository class

import org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository;



public interface BookRepository extends DatastoreRepository<Book, Long>{
}

Here is my domain class

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

and here is my controller class

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";
    }

}

and here is my Application class

@SpringBootApplication
public class Application {

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

}
TorkB80
  • 69
  • 1
  • 6
  • annotate `BookRepository` with `@Repository` – Ryuzaki L Feb 25 '19 at 00:44
  • I added that annotation but still the same error I imported it from `org.springframework.stereotype.Repository` – TorkB80 Feb 25 '19 at 00:49
  • Something you can try is to add ```@Repository("googleBookRepo")``` and then under your @Autowire, add ```@Qualifier("googleBookRepo")``` and see if that works. This isn't an answer but at least it might tell you that you can autowire it and it's something else that could be conflicting. – Hermann Steidel Feb 25 '19 at 01:58
  • what value should I put instead of "googleBookRepo" the name of my project in google cloud? I created a entity called "book" in a default schema – TorkB80 Feb 25 '19 at 02:16
  • I figure this isn't it, but I'll throw it out there...I saw quite a few of these when we converted a project from XML to annotation-based wiring of our beans. We'd somehow end up duplicating the same bean in XML and via an annotation in the code. You don't happen to have any XML-defined beans and maybe you're defining this bean in there too? – CryptoFool May 03 '19 at 04:59

3 Answers3

0

I think the problem is the way you are using the annotation, try changing the Injection to the Constructor, something like:

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

}

Source to understand it: Spring @Autowire on Properties vs Constructor

  • 1
    Why do you think this is going to solve the OPs problem? I believe that either method should work, and I don't think this is going to solve anything. The OP's issue has to do with a conflict when creating a Bean named `bookRepository`. You're just dealing with injecting such a bean, not creating it. – CryptoFool May 03 '19 at 04:54
0

Add this annotation, @EnableDatastoreRepositories, to your Application.java

Jay Yoo
  • 11
  • 4
0

How about using Spring data rest :

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

you won't need to code controllers

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> 

swagger config !!!

@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig {
Tiago Medici
  • 1,944
  • 22
  • 22