我正在学习Spring Boot,但是遇到问题,找不到解决方案。当我尝试运行测试时,出现以下异常。
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImplementation' defined in file []: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'uploadFileServiceImplementation' defined in file []: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'uploadFileServiceImplementation' defined in file []: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我试图将注解放置在不同的位置,但是并不能解决我的问题。我完全不知道应该如何创建ModelMapper bean。也许您会知道如何处理。这是我的代码:
BookServiceImplementation.class
@Service
public class BookServiceImplementation implements BookService {
private BookRepository bookRepository;
private UploadFileService uploadFileService;
private ModelMapper modelMapper;
@Autowired
public BookServiceImplementation(BookRepository bookRepository, UploadFileService uploadFileService,
ModelMapper modelMapper){
this.bookRepository = bookRepository;
this.uploadFileService = uploadFileService;
this.modelMapper = modelMapper;
}
@Override
public void addBook(AddBookResource addBookResource) {
Book book = new Book();
Long coverImageId = addBookResource.getCoverImageId();
Long contentId = addBookResource.getContentId();
UploadFile coverImage = null;
UploadFile bookContent = null;
if (coverImage != null){
coverImage = uploadFileService.findById(coverImageId)
.map(fileResource -> modelMapper.map(fileResource, UploadFile.class))
.orElse(null);
}
if (contentId != null){
bookContent = uploadFileService.findById(contentId)
.map(fileResource -> modelMapper.map(fileResource, UploadFile.class))
.orElse(null);
}
book.setCoverImage(coverImage);
book.setContent(bookContent);
book.setTitle(addBookResource.getTitle());
book.setDescription(addBookResource.getDescription());
book.setCategories(Arrays.stream(addBookResource.getCategories())
.map(Category::new)
.collect(Collectors.toSet()));
bookRepository.save(book);
}
}
UploadFileServiceImplementation.class
@Service
public class UploadFileServiceImplementation implements UploadFileService {
private UploadFileRepository uploadFileRepository;
private ModelMapper modelMapper;
@Autowired
public UploadFileServiceImplementation(UploadFileRepository uploadFileRepository, ModelMapper modelMapper){
this.uploadFileRepository = uploadFileRepository;
this.modelMapper = modelMapper;
}
@Transactional
@Override
public Long save(String filename, byte[] data) {
UploadFile uploadFile = new UploadFile();
uploadFile.setFileName(filename);
uploadFile.setData(data);
UploadFile saved = uploadFileRepository.save(uploadFile);
return saved.getId();
}
@Override
public Optional<FileResource> findById(Long id) {
return uploadFileRepository.findById(id)
.map(file -> modelMapper.map(file, FileResource.class));
}
}
编辑
经过建议的更改后,我仍然得到IllegalStateException
:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookService': Unsatisfied dependency expressed through field 'uploadFileService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'uploadFileService': Unsatisfied dependency expressed through field 'modelMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'uploadFileService': Unsatisfied dependency expressed through field 'modelMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.modelmapper.ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
答案 0 :(得分:0)
步骤1:(如果主应用程序类或配置类中尚不存在)为模型映射器添加> db.reference.find({metadata: {values: {hola: "adeu"}}});
> db.reference.find({metadata: {values: [{hola: "adeu"}]}});
:
@Bean
}
第2步::如何管理它,请在服务提示中进行以下更改:
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
@Service("uploadFileService")
public class UploadFileServiceImplementation implements UploadFileService {
@Autowired
private UploadFileRepository uploadFileRepository;
@Autowired
private ModelMapper modelMapper;
//Remove constructure now.
BookServiceImplementation