spring boot 注入 restTemplate


轉載自:http://blog.csdn.net/liuchuanhong1/article/details/54631080

 

 

package com.chhliu.springboot.restful;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.web.client.RestTemplateBuilder;  
import org.springframework.context.annotation.Bean;  
import org.springframework.web.client.RestTemplate;  
  
@SpringBootApplication  
public class SpringbootRestTemplateApplication {  
    // 啟動的時候要注意,由於我們在controller中注入了RestTemplate,所以啟動的時候需要實例化該類的一個實例  
    @Autowired  
    private RestTemplateBuilder builder;  
  
        // 使用RestTemplateBuilder來實例化RestTemplate對象,spring默認已經注入了RestTemplateBuilder實例  
    @Bean  
    public RestTemplate restTemplate() {  
        return builder.build();  
    }  
  
    public static void main(String[] args) {  
        SpringApplication.run(SpringbootRestTemplateApplication.class, args);  
    }  
}  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM