gpt4 book ai didi

java - NoSuchBeanDefinition 异常

转载 作者:行者123 更新时间:2023-11-30 02:16:24 26 4
gpt4 key购买 nike

在尝试运行我的 Spring Boot 应用程序时,出现以下错误:没有此类 bean 定义异常。请让我知道我错过了什么。它是一个简单的 spring 框架,但我遇到了这个错误。如有任何帮助,我们将不胜感激。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cnanational.servicecommon.validation.Validator<com.cnanational.productservice.model.DealerSeriesAttributesRequest>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)

下面给出的是调用 bean 的服务类

@Service
public class ProductServiceImpl implements ProductService {

private final Logger log = LoggerFactory.getLogger(getClass());

private final Validator<SeriesSurchargesRequest> seriesSurchargesRequestValidator;

private final Validator<DealerSeriesAttributesRequest> dealerSeriesAttributesRequestValidator;

private final ProductServiceDao productDao;

@Value("${spring.application.name}")
private String serviceName;

@Autowired
public ProductServiceImpl(
final ProductServiceDao productDao, final Validator<SeriesSurchargesRequest> seriesSurchargesRequestValidator,
final Validator<DealerSeriesAttributesRequest> dealerSeriesAttributesRequestValidator )
{
this.productDao = productDao;
this.seriesSurchargesRequestValidator = seriesSurchargesRequestValidator;
this.dealerSeriesAttributesRequestValidator = dealerSeriesAttributesRequestValidator;
}

@Override
public SeriesSurcharges getSurcharges(SeriesSurchargesRequest stdOptions) {

this.seriesSurchargesRequestValidator.validateAndThrowIfInvalid(
stdOptions,
"SeriesSurchargesRequest");

SeriesSurcharges charges = productDao.findSurcharges(stdOptions);

return charges;
}

@Override
public List<DealerSeriesAttribute> getDealerSeriesAttributes(DealerSeriesAttributesRequest request) throws Exception {    
   this.dealerSeriesAttributesRequestValidator.validateAndThrowIfInvalid(
request,
"dealerSeriesAttributesRequest");
   
   List <DealerSeriesAttribute> result = new ArrayList<DealerSeriesAttribute>();
   
       for (ProductSeriesFilter psfilter : request.getProductSeriesFilters()) {
    log.debug("getDealerSeriesAttributes-1: ProductSeriesFilter: " + psfilter.toString());
    List<DealerSeriesAttribute> dsa = productDao.getDealerSeriesAttributes( request);
    result.addAll(dsa);
    log.debug("Product service Impl : The size of dsa, sizes={}", dsa.size());
       }       
       return new ArrayList<DealerSeriesAttribute>(result);   
}
}

下面给出的是 Validator 类。

public class CreateDealerSeriesAttributesRequestValidator implements Validator<DealerSeriesAttributesRequest>{

private final SmartValidator smartValidator;

public CreateDealerSeriesAttributesRequestValidator(final SmartValidator smartValidator)
{
this.smartValidator = smartValidator;
}

@Override
public boolean supports(Class<?> clazz) {
return DealerSeriesAttributesRequest.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {}

@Override
public SmartValidator smartValidator() {
return smartValidator;
}
}

最佳答案

我猜你的包 com.cnanational.productservice.model 不在你的入口点所在的包下(用 @SpringBootApplication 注释的类)

Spring需要知道去哪里扫描bean,在spring boot中,默认情况下spring boot会扫描@SpringBootApplication注解的类所在的包以及该包下的包,也可以使用@componantscan指定包您需要扫描,这样您就可以像这样解决您的问题

@SpringBootApplication
@ComponentScan("com.cnanational.productservice.model")

或者将您的 bean 实现放在默认情况下会扫描的包下。

此外,您还需要确保您的类已注册为 bean,因此如果您的 Validator 接口(interface)未注册为 bean,您可以注释 Validator 或 CreateDealerSeriesAttributesRequestValidator @Component 或使用 @Bean 在以下之一下创建 CreateDealerSeriesAttributesRequestValidator 的实例您的配置类。

注意由于您使用的是 Autowiring 接口(interface),因此当您有多个实现时需要小心,您需要使用 @Qualifier 帮助 spring 决定选择哪个实现

关于java - NoSuchBeanDefinition 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48245488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com