[英]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.说明:com.issuemanagement.service.impl.IssueHistoryServiceImpl 中构造函数的参数 0 需要找不到类型为“com.issuemanagement.repository.IssueHistoryRepository”的 bean。
Action: Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' in your configuration.行动:考虑在你的配置中定义一个“com.issuemanagement.repository.IssueHistoryRepository”类型的bean。
IssueHistoryRepository.java IssueHistoryRepository.java
public interface IssueHistoryRepository extends JpaRepository<IssueHistory, Long>{
}
IssueHistoryService.java问题HistoryService.java
public interface IssueHistoryService {
IssueHistory save(IssueHistory issueHistory);
IssueHistory getById(Long id);
Page<IssueHistory> getAllPageable(Pageable pageable);
Boolean delete(IssueHistory issueHistory);
}
IssueHistoryServiceImpl.java问题HistoryServiceImpl.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问题管理应用程序.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();
}
}
You need to mark your IssueHistoryRepository.java
interface with @Repository
annotation.您需要使用@Repository
注释标记您的IssueHistoryRepository.java
接口。 This annotation will allow Spring to automatically detect your bean and to register it in ApplicationContext, from which you can autowire it.此注解将允许 Spring 自动检测您的 bean 并将其注册到 ApplicationContext 中,您可以从中自动装配它。
@SpringBootApplication
encapsulates @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
annotations with their default attributes. @SpringBootApplication
封装了@Configuration
、 @EnableAutoConfiguration
和@ComponentScan
注解及其默认属性。 The default value for @ComponentScan
means that all the sub packages on the package the @ComponentScan
is used are scanned. @ComponentScan
的默认值表示使用@ComponentScan
的package 上的所有子包都被扫描。 That is why it is usually a good practice to include the main class in the base package of the project.这就是为什么在项目的基础 package 中包含主要的 class 通常是一个很好的做法。 Having said that, please drop @ComponentScan
and @EnableAutoConfiguration
from your main class as follows:话虽如此,请从您的主要 class 中删除@ComponentScan
和@EnableAutoConfiguration
,如下所示:
@SpringBootApplication
public class IssueManagementApplication {
public static void main(String[] args) {
SpringApplication.run(IssueManagementApplication.class, args);
}
@Bean
public ModelMapper getModelMapper() {
return new ModelMapper();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.