创建名称为' fieldServiceImpl'的bean时出错在文件[]中定义:通过构造函数参数1

时间:2017-02-13 17:20:11

标签: java spring hibernate

我收到此错误:

  

2017-02-13 11:53:48.497 WARN 13276 --- [restartedMain]   ationConfigEmbeddedWebApplicationContext:遇到异常   在上下文初始化期间 - 取消刷新尝试:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为' fieldServiceImpl'的bean时出错在文件中定义   [... \ BIN \共同\ COM \服务\ IMPL \ FieldServiceImpl.class]:   通过构造函数参数1表示的不满意的依赖性;   嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   属于' co.com.service.mapper.FieldMapper'的限定bean可供选择:   预计至少有1个豆有资格成为autowire候选人。   依赖注释:{}

     

2017-02-13 11:53:48.679 WARN 13276 --- [restartedMain]   o.s.boot.SpringApplication:错误处理失败   (创建名称为' delegatingApplicationListener'定义的bean时出错   在类路径资源中   [组织/ springframework的/安全/配置/注解/网络/配置/ WebSecurityConfiguration.class]:   实例化bean之前的BeanPostProcessor失败;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   使用名称创建bean时出错   ' org.springframework.cache.annotation.ProxyCachingConfiguration&#39 ;:   bean的初始化失败;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   豆命名   ' org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'   可用的)

     

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

           

说明

     

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

FieldService.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);
}

FieldServiceImpl.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);
    }
}

我该怎么办? 提前致谢

1 个答案:

答案 0 :(得分:0)

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

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

或者将autowired放在字段上并删除co​​nsturctor:

@Autowired
private FieldRepository fieldRepository;

@Autowired 
private FieldMapper fieldMapper;

<强> Upate

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

@Component
public class FieldMapperImpl implements FieldMapper{}

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