1,直接调用会错,表示没有没定义bean
@Autowired private RestTemplate restTemplate
//会报 Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
2,需要在config注入bean
@LoadBalanced @Bean public RestTemplate loadBalanced1() { return new RestTemplate(); } 这样也会报错,他会在服务注册中心找,找不到实例No instances available for xxx
3,解决办法,
在配置类
*/
@Configuration
public class RestConfig {
@Bean(name = "remoteRestTemplate")
public RestTemplate restTemplateRemote() {
return new RestTemplate();
}
}
在需要调入的
@Autowired
@Qualifier(value = "remoteRestTemplate")
private RestTemplate rest;
标签:RestTemplate,Bean,private,bean,使用,new,public From: https://www.cnblogs.com/dangwang/p/16998387.html