我想創建一個父/框架模塊並定義一個使用某些接口方法的服務。如何通過父模塊中的接口連接Spring服務?
接口實現應該由實現項目提供。但不知何故注射不起作用。爲什麼?
框架模塊:
package de.test;
@Service
public class BaseServiceExecutor {
@Autowired
private ICustomService service;
public void run() {
service.use();
}
}
interface ICustomService {
void use();
}
@Configuration
public class FrameworkAppConfig {
}
實現模塊:
package de.test.impl;
@Service
public class MyCustomService implements ICustomService {
@Override
void use() {
//custom impl
}
}
appContext.xml:(實施項目內)
<context:component-scan base-package="de.test" />
@Configuration
@Import(FrameworkAppConfig.class)
@ComponentScan("de.test")
public class ImplAppConfig
結果:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [IcustomService] 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)}
也許組件掃描不拿起MyCustomService。 – geoand
我有一個與實現項目中的名稱相同的框架(用於測試)。我的實施項目中的服務全部得到提取。 – membersound
錯誤消息意味着Spring無法找到想要的bean。正如@geoand指出的那樣,看起來像你的豆沒有被掃描檢測到。請發佈每個類/接口的包的名稱,以及如何在beans.xml中掃描它們(或者您稱爲spring xml配置)。 –