在jar文件中注入自动连接的依赖项失败

时间:2017-06-05 12:23:23

标签: java spring

我在我的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)}

1 个答案:

答案 0 :(得分:3)

只要您没有StoreField接口的实现,就无法注入依赖项。确保它是作为一个类实现的,并通过xml或annotation在spring上下文中正确初始化。