Comment Autocâblés dans ConversionService dans springboot
Tente d'accéder à la ConversionControl dans le modèle en springboot, pas de chance.
@Component
public class CityHelperService {
@Autowired
ConversionService conversionService;//= ConversionServiceFactory.registerConverters();
public City toEntity(CityDTO dto){
City entity = conversionService.convert(dto, City.class);
return entity;
}
public CityDTO toDTO(City entity){
CityDTO dto = conversionService.convert(entity, CityDTO.class);
return dto;
}
}
Il affiche l'erreur suivante:
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lumiin.mytalk.model.CityModel com.lumiin.mytalk.controllers.CityController.cityModel;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityModel' defined in file : Unsatisfied dependency expressed through constructor argument with index 1 of type [com.lumiin.mytalk.dao.CityHelperService]: : Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)};
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
OriginalL'auteur lumi | 2015-05-04
Vous devez vous connecter pour publier un commentaire.
Il n'est apparemment pas
ConversionService
bean disponible, à en juger par la dernière exception imbriquée:Un coup d'oeil dans le Le printemps de la documentation révèle, que vous devez déclarer un
ConversionService
bean. Dans la configuration XML, il devrait ressembler à ceci:Et puisque vous êtes à l'aide de Spring Boot, je suppose que vous êtes à créer le contexte par programmation, vous devez donc créer une méthode annotée avec
@Bean
, qui renvoie unConverstionService
, comme ceci (expliqué ici):Vous n'avez probablement pas spécifier un convertisseur - vous devez créer une classe qui implémente l'interface
Converter<City, CityDTO>
, et un autre dans l'autre sens, puis s'inscrire à la fois dans leConversionServiceFactoryBean
. Veuillez vous référer à ce tutoriel: javabeat.net/introduction-to-spring-converters-and-formattersDans le Javaconfig, Vous n'avez même pas besoin de retourner un
ConverstionService
de laConversionServiceFactoryBean
comme dans l'exemple ci-dessus, il suffit de revenir unConversionServiceFactoryBean
haricot et le printemps fera le reste pour vousOriginalL'auteur lmazgon
Pas tout à fait d'accord avec les réponses acceptées, parce qu'il y aurait un défaut
ConverstionService
nommémvcConversionService
donc, vous obtiendrez en double haricot exception. Au lieu de celaaddConverter
àFormatterRegistry
, voici le lien pour la partie réponse:Java Config équivalente pour conversionService /FormattingConversionServiceFactorybean
Aussi vous auriez besoin (dans certains cas) pour définir au moins un vide
Component
pourConversionService
, quelque chose comme ci-dessous:C'est à force de printemps conteneur pour lancer un appel:
mvcConversionService
ne sera présent que si vous avezorg.springframework.boot:spring-boot-starter-web
de la dépendance dans votre projetOriginalL'auteur vijay