Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
  }
}
like image 301
Aymen Kanzari Avatar asked May 18 '18 13:05

Aymen Kanzari


People also ask

Can a constructor have a parameter list?

Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.

What is constructor injection Spring boot?

Constructor Based Dependency Injection. It is a type of Spring Dependency Injection, where object's constructor is used to inject dependencies. This type of injection is safer as the objects won't get created if the dependencies aren't available or dependencies cannot be resolved.

What is constructor Dependency injection?

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.

Why we use constructor injection in Spring?

Constructor injection helps in creating immutable objects because a constructor's signature is the only possible way to create objects. Once we create a bean, we cannot alter its dependencies anymore.


2 Answers

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.

like image 70
Always Learning Avatar answered Oct 11 '22 09:10

Always Learning


I solved it with this configuration. Look for @EnableAutoConfiguration in your Configuration file.

@Configuration

@EnableJpaRepositories(basePackages = "com.akog02.repostories")

@EntityScan(basePackages = "com.akog02.domain")

@EnableTransactionManagement

@EnableAutoConfiguration

public class WebConfiguration {

}
like image 25
Mr Kyaing Avatar answered Oct 11 '22 08:10

Mr Kyaing