2017-02-13 76 views
1

我收到此錯誤:錯誤創建名稱爲豆「fieldServiceImpl」在文件中定義[]:不滿意的依賴,通過構造函數的參數表示1

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.

我有這個類: 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); 
} 

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

我該怎麼辦? 在此先感謝

回答

0

正如你在類中定義一個2參數consturctor,你應該自動裝配的依賴關係,使Spring知道什麼注入以及如何調用構造函數:

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

或者放自動裝配在字段,並刪除任何的consturctor:

@Autowired 
private FieldRepository fieldRepository; 

@Autowired 
private FieldMapper fieldMapper; 

Upate

另外,我覺得你需要有FieldMapper接口的實現的地方,並應與@Component還是@Service註解來標註:

@Component 
public class FieldMapperImpl implements FieldMapper{} 

,因爲它似乎沒有實現在春季Cotnext註冊。

相關問題