【问题标题】:Spring Boot - Can't handle IllegalStateExceptionSpring Boot - 无法处理 IllegalStateException
【发布时间】:2018-09-22 20:56:48
【问题描述】:

我正在学习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)}

【问题讨论】:

    标签: spring-boot illegalstateexception modelmapper


    【解决方案1】:

    第 1 步:为模型映射器添加 @Bean(如果主应用程序类或配置类中尚未存在):

    @SpringBootApplication
    public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }
    

    }

    第 2 步:这是您如何管理它,请在服务实现中进行以下更改:

    @Service("uploadFileService")
    public class UploadFileServiceImplementation implements UploadFileService {
    
       @Autowired
       private UploadFileRepository uploadFileRepository;
    
       @Autowired
       private ModelMapper modelMapper;
    
      //Remove constructure now.
    

    BookServiceImplementation 也一样

    @Service("bookService")
    public class BookServiceImplementation implements BookService {
    
       @Autowired
       private BookRepository bookRepository;
    
       @Autowired
       private UploadFileService uploadFileService;
    
       @Autowired
      private ModelMapper modelMapper;
    
    //remove constructure
    

    【讨论】:

    • 我已经按照您的建议进行了更改,但仍然出现异常。
    • 我编辑了我的帖子,在建议的更改后添加了一条错误消息。
    • 你能把你的代码分享到 github 并告诉我 url,让我看清楚..
    • 酷让我看看,同时检查您是否定义了模型映射器 bean,请查看我的更新答案
    • @mariusz 检查我更新的答案为 Application 类中的模型映射器定义 Bean,你会很好..
    猜你喜欢
    • 2021-03-23
    • 2017-10-28
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2019-09-08
    • 2020-04-27
    • 2018-11-13
    相关资源
    最近更新 更多