SpringBoot,Springmvc Spring 知識總結

瓜瓜東西發表於2018-08-28

 


1 非常經典的初始化resttemplate的方法


  通過配置加上 條件註解實現,實現功能有設定超時引數,修改傳遞編碼,新增攔截器等


@Configuration
@ConditionalOnClass(value = {RestTemplate.class, HttpClient.class})
public class RestTemplateConfiguration {
 
  @Value("${remote.maxTotalConnect:0}")
  private int maxTotalConnect; //連線池的最大連線數預設為0
  @Value("${remote.maxConnectPerRoute:200}")
  private int maxConnectPerRoute; //單個主機的最大連線數
  @Value("${remote.connectTimeout:2000}")
  private int connectTimeout; //連線超時預設2s
  @Value("${remote.readTimeout:30000}")
  private int readTimeout; //讀取超時預設30s
 
  //建立HTTP客戶端工廠
  private ClientHttpRequestFactory createFactory() {
    if (this.maxTotalConnect <= 0) {
      SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
      factory.setConnectTimeout(this.connectTimeout);
      factory.setReadTimeout(this.readTimeout);
      return factory;
    }
    HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect)
        .setMaxConnPerRoute(this.maxConnectPerRoute).build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
        httpClient);
    factory.setConnectTimeout(this.connectTimeout);
    factory.setReadTimeout(this.readTimeout);
    return factory;
  }
 
  //初始化RestTemplate,並加入spring的Bean工廠,由spring統一管理
  @Bean
  @ConditionalOnMissingBean(RestTemplate.class)
  public RestTemplate getRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(this.createFactory());
    List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
 
    //重新設定StringHttpMessageConverter字符集為UTF-8,解決中文亂碼問題
    HttpMessageConverter<?> converterTarget = null;
    for (HttpMessageConverter<?> item : converterList) {
      if (StringHttpMessageConverter.class == item.getClass()) {
        converterTarget = item;
        break;
      }
    }
    if (null != converterTarget) {
      converterList.remove(converterTarget);
    }
    converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
 
    //加入FastJson轉換器 根據使用情況進行操作,此段註釋,預設使用jackson
    //converterList.add(new FastJsonHttpMessageConverter4());
    return restTemplate;
  }
 
     private void checkAuthHeaderExist(){
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        boolean hasBasicAuth = false;
        for (int i = 0; i < interceptors.size(); i++) {
            if (interceptors.get(i)  instanceof BasicAuthorizationInterceptor) {
                hasBasicAuth =true;
            }
        }
        if(!hasBasicAuth){
            restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username,password));
        }
 
}

 

2 Spring 非同步程式設計

 

主要用到了兩個註解 @EnableAsync用在配置上  @Async(用在 servicer主類方法裡)

步驟有3步驟

a:

@Configure //等價於 spring.xml裡面的 配置

@EnableAsnyn

@ComponentScna(‘com.abc’)//等價於spring.xml裡面的包掃描

Configure類

b  @Service

  class MyService{

@Async

a(){}

@Async

b(){}

}

3 主測試類

myService.a();b();執行發現ab列印內容迴圈展示

相關文章