比方說,我用Spring爲我的Java項目,我有以下的接口和類:春輪廓注射:不注射類沒有個人資料
public interface MyInterface { ... }
@Component
public class MyInterfaceMainImpl implements MyInterface { ... }
@Component
@Profile("mock")
public class MyInterfaceMockImpl implements MyInterface { ... }
@ContextConfiguration(locations = {"classpath:my-context.xml"})
@ActiveProfiles(profiles = {"mock"})
public class MyInterfaceTest extends AbstractTestNGSpringContextTests {
@Inject
private MyInterface myInterface;
...
}
假設我的上下文。 xml在包含我的接口及其實現類的包上啓用組件掃描。當我將配置文件指定爲「模擬」時,出現如下錯誤:「期望單個匹配的bean,但找到2:...」。
任何想法如何我可以避免讓我的非配置文件方法在注入期間成爲一個匹配的bean?或者唯一可能的解決方案是給這個主實現類配置一個配置文件?這是我試圖避免的解決方案。