我有一个名为HttpClient
的接口,以及两个beans
bean的实现,
public interface HttpClient {
String bla();
}
@Component
public class HttpClientImpl implements HttpClient {
@Override
public String bla() {
return null;
}
}
@Component
public class HttpClientMock implements HttpClient {
@Override
public String bla() {
return null;
}
}
现在我有一个注入HttpClient的bean的包装器
@Component
public class Wrapper {
@Autowired HttpClient httpClient;
}
当我注入Wrapper
@Autowired Wrapper wrapper;
选择HttpClient
的具体实现,我的意思是如果我要注入HttpClientMock
?
答案 0 :(得分:2)
@Component("httpClient")
public class HttpClientImpl implements HttpClient {
@Override
public String bla() {
return null;
}
}
@Component("httpClientMock")
public class HttpClientMock implements HttpClient {
@Override
public String bla() {
return null;
}
}
@Component
public class Wrapper {
@Autowired
@Qualifier("httpClient")
HttpClient httpClient;
@Autowired
@Qualifier("httpClientMock")
HttpClient httpClientMock;
}
这里似乎你需要你的httpClient进行测试,在这种情况下我不会说要做上面的方法,而是创建如下所示的配置文件(你可以为其他的配置文件有不同的配置文件),当你运行spring应用程序时,运行它在测试配置文件中查找有关spring profiles here
的更多信息@Component("httpClientMock")
@Profile("test")
public class HttpClientMock implements HttpClient {
@Override
public String bla() {
return null;
}
}