gpt4 book ai didi

java - 在 jar 文件中注入(inject) Autowiring 的依赖项失败

转载 作者:行者123 更新时间:2023-11-30 07:54:00 26 4
gpt4 key购买 nike

我在我的 war 文件的 lib 文件夹下放了一个 jar 文件。在 jar 里,我有一些带有@Autowired 文件的类。在 jar 中,在我给出的 applicationContext xml 中

<context:component-scan base-package="com.main.java.mypath" />

代码:

package com.main.java.mypath.client;

@Component
public class ServiceProvider {

@Autowired
private StoreField storeField;




package com.main.java.mypath.data;
public interface StoreField {
}

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceProvider': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.main.java.mypath.data.StoreField com.main.java.mypath.client.ServiceProvider.storeField; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.main.java.mypath.data.StoreField] 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)}

最佳答案

只要您没有StoreField 接口(interface)的实现,就不可能注入(inject)依赖项。确保它作为类实现并通过 xml 或注释在 spring 上下文中正确初始化。

关于java - 在 jar 文件中注入(inject) Autowiring 的依赖项失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44368872/

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