Home > front end >  Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' i
Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' i

Time:02-02

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

}

CodePudding user response:

You need to mark your IssueHistoryRepository.java interface with @Repository annotation. This annotation will allow Spring to automatically detect your bean and to register it in ApplicationContext, from which you can autowire it.

CodePudding user response:

@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();
    }
}
  •  Tags:  
  • Related