Skip to content
Advertisement

Parameter 0 of constructor in ….. Spring Boot

I have a problem when launch my app. Could somebody help me to solve this issue?

Parameter 0 of constructor in com.journaldev.elasticsearch.service.BookServiceImpl required a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' that could not be found.
Action:
Consider defining a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' in your configuration.

GenericRepository

public interface GenericRepository<T, K> {
  Map<String, Object> get(final K id);
}

GenericRepositoryImpl

public class GenericRepositoryImpl<T, K extends Serializable> implements GenericRepository<T, K> { 

  private RestHighLevelClient restHighLevelClient;

  private ObjectMapper objectMapper;

  public GenericRepositoryImpl(ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
    this.objectMapper = objectMapper;
    this.restHighLevelClient = restHighLevelClient;
  }

  @Override
  public Map<String, Object> get(K id) {
    return null;
  }
}

BookRepositorySearch

@Component
public interface BookRepositorySearch extends GenericRepository<Book, Long> {}

BookService

public interface BookService {
  Map<String, Object> get(final Long id);    
}

BookServiceImpl

@Service
public class BookServiceImpl implements BookService {

  private final BookRepositorySearch bookRepositorySearch;

  public BookServiceImpl(BookRepositorySearch bookRepositorySearch) {
    this.bookRepositorySearch = bookRepositorySearch;
  }      
  @Override
  public Map<String, Object> get(Long id) {
    return null;
  }
}

Advertisement

Answer

From your previous comments, looks like you want to keep BookRepositorySearch as an interface. If that’s the case, you need to create a concrete instance of that interface and put @Component on that.

You don’t need @Component on your interface declaration and you can’t extend a class in an interface.

public interface BookRepositorySearch {}

Create a concrete type that implements the interface and extends extends GenericRepository<Book, Long> you want to autowire and put @Component on it:

@Component
public class BookRepositorySearchImpl 
    implements BookRepositorySearch 
    extends GenericRepository<Book, Long>  
{}

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class-ctor

Instantiation with a constructor

When you create a bean by the constructor approach, all normal classes are usable by and compatible with Spring. That is, the class being developed does not need to implement any specific interfaces or to be coded in a specific fashion. Simply specifying the bean class should suffice. However, depending on what type of IoC you use for that specific bean, you may need a default (empty) constructor.

The Spring IoC container can manage virtually any class you want it to manage; it is not limited to managing true JavaBeans. Most Spring users prefer actual JavaBeans with only a default (no-argument) constructor and appropriate setters and getters modeled after the properties in the container. You can also have more exotic non-bean-style classes in your container. If, for example, you need to use a legacy connection pool that absolutely does not adhere to the JavaBean specification, Spring can manage it as well.

6 People found this is helpful
Advertisement