Spring 无法自动装配依赖项
Spring is not able to autowire a dependency
I have an application which is not a dynamic web project. I have the arrangement such that its like a library in form of a jar which is exposed via interfaces. I am trying to auto wire this interface into my API project which is Dynamic web project. But it throws bean creation exception
1. I have a MyLibraryCofiguration class in library which has @Configuration and @Import alongwith @ComponentScan.
@配置
@Import({ BasicConfiguration.class, OperationalLoggingConfiguration.class, RestConfiguration.class })
@ComponentScan("nl.ming.cram")
public class 我的配置 () {
public MyConfiguration() {
packages("nl.ming.cram.gateway");
}
@Bean
public MyInterface getMyInterface() {
return new MyImpl();
}
}
2. My API project has MyApiCofiguration class which has @Import in which I am importing my MyLibraryCofiguration class. In the same class I have used:
@Configuration
@Import({
MyConfiguration.class
})
@ComponentScan({"nl.ming.api.creditcardlist"})
public class CreditCardListConfiguration {
@Bean
public MyInterface getMyInterface() {
return new CramImpl();
}
}
3. In my API project , in MyApiService class - I have @Autowired MyInterface to access methods of library jar as shown below:
@Component
public class MyAPIService {
@Autowired
MyInterface MyInterface;
}
It throws a Bean creation exception for getMyInterface as is shown below -
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'creditCardService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
nl.ing.cram.gateway.CramInterfacenl.ing.api.creditcardlist.services.CreditCardService.cramIface;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCramInterface': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private nl.ing.cram.dao.CramDaonl.ing.cram.gateway.CramImpl.cramDao;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cramDao': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [nl.ing.sc.customerrequest.createcustomerrequest1.CreateCustomerRequestServiceOperationClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
Any help would be appreciated.
没有 spring 可以用于任何类型的 Java 应用程序。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
我能看到上面的异常,这意味着 spring 能够找到您注入的类型,但不能找到它的实现。
检查您的 spring-config :-
<context:component-scan base-package="..." />
异常堆栈跟踪中的异常原因:-
在您的 nl.ing.cram.gateway.CramImpl.cramDao class 中,它未能注入 CreateCustomerRequestServiceOperationClient 类型的依赖项,因为它无法找到其实现。
I have an application which is not a dynamic web project. I have the arrangement such that its like a library in form of a jar which is exposed via interfaces. I am trying to auto wire this interface into my API project which is Dynamic web project. But it throws bean creation exception
1. I have a MyLibraryCofiguration class in library which has @Configuration and @Import alongwith @ComponentScan.
@配置 @Import({ BasicConfiguration.class, OperationalLoggingConfiguration.class, RestConfiguration.class }) @ComponentScan("nl.ming.cram") public class 我的配置 () {
public MyConfiguration() {
packages("nl.ming.cram.gateway");
}
@Bean
public MyInterface getMyInterface() {
return new MyImpl();
}
}
2. My API project has MyApiCofiguration class which has @Import in which I am importing my MyLibraryCofiguration class. In the same class I have used:
@Configuration
@Import({
MyConfiguration.class
})
@ComponentScan({"nl.ming.api.creditcardlist"})
public class CreditCardListConfiguration {
@Bean
public MyInterface getMyInterface() {
return new CramImpl();
}
}
3. In my API project , in MyApiService class - I have @Autowired MyInterface to access methods of library jar as shown below:
@Component
public class MyAPIService {
@Autowired
MyInterface MyInterface;
}
It throws a Bean creation exception for getMyInterface as is shown below -
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'creditCardService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
nl.ing.cram.gateway.CramInterfacenl.ing.api.creditcardlist.services.CreditCardService.cramIface;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCramInterface': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private nl.ing.cram.dao.CramDaonl.ing.cram.gateway.CramImpl.cramDao;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cramDao': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [nl.ing.sc.customerrequest.createcustomerrequest1.CreateCustomerRequestServiceOperationClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
Any help would be appreciated.
没有 spring 可以用于任何类型的 Java 应用程序。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
我能看到上面的异常,这意味着 spring 能够找到您注入的类型,但不能找到它的实现。
检查您的 spring-config :-
<context:component-scan base-package="..." />
异常堆栈跟踪中的异常原因:-
在您的 nl.ing.cram.gateway.CramImpl.cramDao class 中,它未能注入 CreateCustomerRequestServiceOperationClient 类型的依赖项,因为它无法找到其实现。