'Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' in your configuration
I get this error. What can be done in this case?
APPLICATION FAILED TO START
Description: Parameter 0 of constructor in com.issuemanagement.service.impl.IssueHistoryServiceImpl required a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' that could not be found.
Action: Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' in your configuration.
IssueHistoryRepository.java
public interface IssueHistoryRepository extends JpaRepository<IssueHistory, Long>{
}
IssueHistoryService.java
public interface IssueHistoryService {
IssueHistory save(IssueHistory issueHistory);
IssueHistory getById(Long id);
Page<IssueHistory> getAllPageable(Pageable pageable);
Boolean delete(IssueHistory issueHistory);
}
IssueHistoryServiceImpl.java
@Service
public class IssueHistoryServiceImpl implements IssueHistoryService {
private final IssueHistoryRepository issueHistoryRepository;
private final ModelMapper modelMapper;
public IssueHistoryServiceImpl(IssueHistoryRepository issueHistoryRepository, ModelMapper modelMapper) {
this.issueHistoryRepository = issueHistoryRepository;
this.modelMapper = modelMapper;
}
@Override
public IssueHistory save(IssueHistory issueHistory) {
if(issueHistory.getDate()==null) {
throw new IllegalArgumentException("Issue cannot be null");
}
issueHistory = issueHistoryRepository.save(issueHistory);
return issueHistory;
}
@Override
public IssueHistory getById(Long id) {
return issueHistoryRepository.getOne(id);
}
@Override
public Page<IssueHistory> getAllPageable(Pageable pageable) {
return issueHistoryRepository.findAll(pageable);
}
@Override
public Boolean delete(IssueHistory issueHistory) {
issueHistoryRepository.delete(issueHistory);
return Boolean.TRUE;
}
}
IssueManagementApplication.java
@SpringBootApplication
@ComponentScan({"com.issuemanagement"})
@EnableAutoConfiguration
public class IssueManagementApplication {
public static void main(String[] args) {
SpringApplication.run(IssueManagementApplication.class, args);
}
@Bean
public ModelMapper getModelMapper() {
return new ModelMapper();
}
}
Solution 1:[1]
@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project. Having said that, please drop @ComponentScan and @EnableAutoConfiguration from your main class as follows:
@SpringBootApplication
public class IssueManagementApplication {
public static void main(String[] args) {
SpringApplication.run(IssueManagementApplication.class, args);
}
@Bean
public ModelMapper getModelMapper() {
return new ModelMapper();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | João Dias |
