springboot RestTemplate 注入

来源:互联网 发布:上瘾网络剧百度云1-20 编辑:程序博客网 时间:2024/05/16 15:13

  1. springboot是无法自动注入RestTemplate ,但是springCloud可以自动注入,这样子我们要 在初始化springboot配置类中注入RestTemplate ,这样子在其他class中 @Autowired RestTemplate restUtil就不会有问题了,代码示例如下所示。
@EnableAutoConfiguration@ComponentScan(basePackages = {"cn.creditease"})@EntityScan(basePackages="cn.creditease.entity")@EnableJpaRepositories(basePackages="cn.creditease.reposirory")public class ServletInitializer extends SpringBootServletInitializer {    @Bean    public RestTemplate restTemplate(RestTemplateBuilder builder) {        return builder.build();    }    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(ServletInitializer.class);    }}
0 0
原创粉丝点击