java - 创建文件 [] : Unsatisfied dependency expressed through constructor parameter 1 中定义的名称为 'fieldServiceImpl' 的 bean 时出错

标签 java spring hibernate

我收到此错误:

2017-02-13 11:53:48.497 WARN 13276 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fieldServiceImpl' defined in file [...\bin\co\com\service\impl\FieldServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'co.com.service.mapper.FieldMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

2017-02-13 11:53:48.679 WARN 13276 --- [ restartedMain] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)

*************************** APPLICATION FAILED TO START


Description:

Parameter 1 of constructor in co.com.service.impl.FieldServiceImpl required a bean of type 'co.com.service.mapper.FieldMapper' that could not be found.

Action:

Consider defining a bean of type 'co.com.service.mapper.FieldMapper' in your configuration.

我有这样的类(class): 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);
    }
}

我该怎么办? 提前致谢

最佳答案

由于您在类中定义了一个 2 参数构造函数,因此您应该 Autowiring 依赖项,以便 Spring 知道要注入(inject)什么以及如何调用构造函数:

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

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

@Autowired
private FieldRepository fieldRepository;

@Autowired 
private FieldMapper fieldMapper;

更新

另外,我认为您需要在 FieldMapper 接口(interface)的某个地方实现,并且应该用 @Component 或 @Service 注释来标记它:

@Component
public class FieldMapperImpl implements FieldMapper{}

似乎 Spring Cotnext 中没有注册任何实现。

关于java - 创建文件 [] : Unsatisfied dependency expressed through constructor parameter 1 中定义的名称为 'fieldServiceImpl' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42209824/

相关文章:

java - GWT - RPC 传输 Hibernate 对象问题

java - 保存关系的一侧时出现 LazyInitializationException

java - Hibernate OneToOne 关系 java.lang.NullPointerException

java - 使用开始和结束偏移突出显示 JTextArea 中特定行上的文本

java - JPA查询问题

java - spring-el线程中的SpelExpression安全吗?

java - 使用camel,如何使用 header 的值来定义 XPath 表达式?

java - HttpsURLConnection 验证两次?

java - 在 netbeans 中使用 .class 文件

java - Java中的时区