SpringBoot,Springmvc Spring 知識總結
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列印內容迴圈展示
相關文章
- Spring知識總結1:spring開發流程Spring
- servlet知識總結Servlet
- Cookie知識總結(-)Cookie
- MySQL知識總結MySql
- 知識點總結
- 知識方法總結
- Docker知識總結Docker
- JQuery知識總結jQuery
- Redis知識總結Redis
- 圖知識總結
- golang知識總結Golang
- 常量知識總結
- servelt知識總結
- Spring/SpringBoot常用註解總結Spring Boot
- Spring和Springboot相關知識點整理Spring Boot
- SpringMVC+Spring+Mybatis配置的簡要總結SpringMVCMyBatis
- SpringMVC總結SpringMVC
- Java 知識點總結Java
- Vue知識總結(2)Vue
- django知識點總結Django
- iOS 知識點總結iOS
- MongoDB知識點總結MongoDB
- HDFS知識點總結
- HBase知識點總結
- jQuery 知識點總結jQuery
- Kafka知識點總結Kafka
- Tomcat 知識點總結Tomcat
- MySQL知識點總結MySql
- 概率論知識總結
- mybatis、spring、springMVC、springboot的原始碼MyBatisSpringMVCSpring Boot原始碼
- JavaWeb基礎知識總結:如何系統學習spring boot?JavaWebSpring Boot
- 前端知識點總結——Vue前端Vue
- 移動前端知識總結前端
- Java基礎知識總結Java
- React 基礎知識總結React
- 知識點漏缺總結
- JS知識總結之事件JS事件
- jQuery知識總結之事件jQuery事件