创建名为'fieldServiceImpl'的bean在文件[]中定义时出错:通过构造函数参数1表示的不满意依赖性

问题描述 投票:1回答:1

我收到此错误:

2017-02-13 11:53:48.497 WARN 13276 --- [restartedMain] ationConfigEmbeddedWebApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'fieldServiceImpl'的bean时出错file [... \ bin \ co \ com \ service \ impl \ FieldServiceImpl.class]:通过构造函数参数1表示的不满意的依赖;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'co.com.service.mapper.FieldMapper'类型的限定bean可用:预期至少有1个bean有资格作为autowire候选者。依赖注释:{}

2017-02-13 11:53:48.679 WARN 13276 --- [restartedMain] osboot.SpringApplication:错误处理失败(在类路径资源中定义名为'delegatingApplicationListener'的bean时出错[org / springframework / security / config / annotation /web/configuration/WebSecurityConfiguration.class]:bean实例化之前的BeanPostProcessor失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.cache.annotation.ProxyCachingConfiguration'的bean时出错:bean的初始化失败;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'的bean可用)

***************************申请失败


描述:

co.com.service.impl.FieldServiceImpl中构造函数的参数1需要一个无法找到的类型为'co.com.service.mapper.FieldMapper'的bean。

行动:

考虑在配置中定义类型为'co.com.service.mapper.FieldMapper'的bean。

我有这个类:FieldMapper.java

package co.com.service.mapper;

import co.com.domain.*;
import co.com.service.dto.FieldDTO;

import org.mapstruct.*;
import java.util.List;

/**
 * Mapper for the entity Field and its DTO FieldDTO.
 */
@Mapper(componentModel = "spring", uses = {})
public interface FieldMapper {

    FieldDTO fieldToFieldDTO(Field field);

    List<FieldDTO> fieldsToFieldDTOs(List<Field> fields);

    @Mapping(target = "productionOrderFields", ignore = true)
    Field fieldDTOToField(FieldDTO fieldDTO);

    List<Field> fieldDTOsToFields(List<FieldDTO> fieldDTOs);
}

field service.Java

package co.com.service;

import co.com.service.dto.FieldDTO;
import java.util.List;

/**
 * Service Interface for managing Field.
 */
public interface FieldService {

    /**
     * Save a field.
     *
     * @param fieldDTO the entity to save
     * @return the persisted entity
     */
    FieldDTO save(FieldDTO fieldDTO);

    /**
     *  Get all the fields.
     *  
     *  @return the list of entities
     */
    List<FieldDTO> findAll();

    /**
     *  Get the "id" field.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    FieldDTO findOne(Long id);

    /**
     *  Delete the "id" field.
     *
     *  @param id the id of the entity
     */
    void delete(Long id);
}

field service imp了.Java

package co.com.service.impl;

import co.com.service.FieldService;
import co.com.domain.Field;
import co.com.repository.FieldRepository;
import co.com.service.dto.FieldDTO;
import co.com.service.mapper.FieldMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Field.
 */
@Service
@Transactional
public class FieldServiceImpl implements FieldService{

    private final Logger log = LoggerFactory.getLogger(FieldServiceImpl.class);

    private final FieldRepository fieldRepository;

    private final FieldMapper fieldMapper;

    public FieldServiceImpl(FieldRepository fieldRepository, FieldMapper fieldMapper) {
        this.fieldRepository = fieldRepository;
        this.fieldMapper = fieldMapper;
    }

    /**
     * Save a field.
     *
     * @param fieldDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public FieldDTO save(FieldDTO fieldDTO) {
        log.debug("Request to save Field : {}", fieldDTO);
        Field field = fieldMapper.fieldDTOToField(fieldDTO);
        field = fieldRepository.save(field);
        FieldDTO result = fieldMapper.fieldToFieldDTO(field);
        return result;
    }

    /**
     *  Get all the fields.
     *  
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<FieldDTO> findAll() {
        log.debug("Request to get all Fields");
        List<FieldDTO> result = fieldRepository.findAll().stream()
            .map(fieldMapper::fieldToFieldDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one field by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public FieldDTO findOne(Long id) {
        log.debug("Request to get Field : {}", id);
        Field field = fieldRepository.findOne(id);
        FieldDTO fieldDTO = fieldMapper.fieldToFieldDTO(field);
        return fieldDTO;
    }

    /**
     *  Delete the  field by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Field : {}", id);
        fieldRepository.delete(id);
    }
}

我该怎么办?提前致谢

java spring hibernate
1个回答
0
投票

正如您在类中定义了一个2参数构造函数一样,您应该自动装配依赖项,以便Spring知道要注入的内容以及如何调用构造函数:

@Autowired
public FieldServiceImpl(FieldRepository fieldRepository, FieldMapper fieldMapper) {
        this.fieldRepository = fieldRepository;
        this.fieldMapper = fieldMapper;
}

或者将autowired放在字段上并删除构造函数:

@Autowired
private FieldRepository fieldRepository;

@Autowired 
private FieldMapper fieldMapper;

得到它

此外,我认为你需要在FieldMapper接口的某个地方有一个实现,它应该用@Component或@Service注释标记:

@Component
public class FieldMapperImpl implements FieldMapper{}

因为似乎没有在Spring Context中注册实现。

© www.soinside.com 2019 - 2024. All rights reserved.