默認情況下,spring boot web 應用會裝配一些功能組件 bean。
在大多數 web 應用場景下,可以選擇性地關閉一下自動裝配的spring 組件 bean,以達到提升性能的目的。
配置項優化
spring boot web 應用加速 完整配置項
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
management.add-application-context-header = false spring.mvc.formcontent.putfilter.enabled = false spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\ org.springframework.boot.autoconfigure.jmx.jmxautoconfiguration,\ org.springframework.boot.autoconfigure.gson.gsonautoconfiguration,\ org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration,\ org.springframework.boot.autoconfigure.jdbc.xadatasourceautoconfiguration,\ org.springframework.boot.autoconfigure.jdbc.jndidatasourceautoconfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.jtaautoconfiguration,\ org.springframework.boot.autoconfigure.websocket.websocketautoconfiguration,\ org.springframework.boot.autoconfigure.websocket.websocketmessagingautoconfiguration,\ org.springframework.boot.autoconfigure.freemarker.freemarkerautoconfiguration,\ org.springframework.boot.autoconfigure.groovy.template.groovytemplateautoconfiguration,\ org.springframework.boot.autoconfigure.mustache.mustacheautoconfiguration,\ org.springframework.boot.autoconfigure.mail.mailsenderautoconfiguration,\ org.springframework.boot.autoconfigure.mail.mailsendervalidatorautoconfiguration,\ org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration,\ org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration,\ org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration |
配置項匯總
1
2
3
|
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration,\ org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration,\ org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration |
關閉 web 請求跟蹤 自動裝配
org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration
顧名思義,該自動裝配用跟蹤 web 請求,通過servlet filter org.springframework.boot.actuate.trace.webrequesttracefilter
記錄請求的信息(如:請求方法、請求頭以及請求路徑等),其計算的過程存在一定的開銷,使用場景罕見,故可選擇關閉。
配置項
1
|
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration |
org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration
當org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration
關閉后,其請求信息存儲介質org.springframework.boot.actuate.trace.tracerepository
沒有存在的必要,故可選擇關閉。
配置項
1
|
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration |
關閉 web 請求結果指標 自動裝配
org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration
該組件將自動裝配org.springframework.boot.actuate.autoconfigure.metricsfilter
,該 filter主要記錄web 請求結果指標(如:相應狀態碼、請求方法執行時間等),該信息一定程度上與反向代理服務器(nginx)功能重疊,故可選擇關閉。
配置項
1
|
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration |
可關閉 servlet web 組件
org.springframework.web.filter.httpputformcontentfilter
引入版本
org.springframework.web.filter.httpputformcontentfilter
由 spring framework 3.1 版本引入,分發在 org.springframework:spring-web
中。
使用場景
通常 web 場景中,瀏覽器通過 http get 或者 post 請求 提交 form 數據,而非瀏覽器客戶端(如應用程序)可能通過 http put 請求來實現。
當 http 請求頭content-type 為 application/x-www-form-urlencoded 時,form 數據被 encoded。而 servlet 規范中, servletrequest.getparameter*()方法僅對 http post 方法支持請求參數的獲取,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public intetfacce servletrequest { ...... public string getparameter(string name); public enumeration<string> getparameternames(); public string[] getparametervalues(string name); public map<string, string[]> getparametermap(); ...... } |
故 以上方法無法支持 http put 或 http patch 請求方法(請求頭content-type
為application/x-www-form-urlencoded
)。
org.springframework.web.filter.httpputformcontentfilter
正是這種場景的解決方案。
spring boot 默認場景下,將org.springframework.web.filter.httpputformcontentfilter
被org.springframework.boot.autoconfigure.web.webmvcautoconfiguration
自動裝配,以下為 spring boot1.4.1.release 以及更好版本定義(可能存在一定的差異):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@configuration @conditionalonwebapplication @conditionalonclass ({ servlet. class , dispatcherservlet. class , webmvcconfigureradapter. class }) @conditionalonmissingbean (webmvcconfigurationsupport. class ) @autoconfigureorder (ordered.highest_precedence + 10 ) @autoconfigureafter ({ dispatcherservletautoconfiguration. class , validationautoconfiguration. class }) public class webmvcautoconfiguration { ...... @bean @conditionalonmissingbean (httpputformcontentfilter. class ) @conditionalonproperty (prefix = "spring.mvc.formcontent.putfilter" , name = "enabled" , matchifmissing = true ) public orderedhttpputformcontentfilter httpputformcontentfilter() { return new orderedhttpputformcontentfilter(); } ...... } |
綜上所述,org.springframework.web.filter.httpputformcontentfilter
在絕大多數 web 使用場景下為非必須組件。
配置項
如果應用依賴 spring boot 版本 為 1.4.1.release 以及更高的版本,可通過如下配置,進行將 org.springframework.web.filter.httpputformcontentfilter
關閉:
1
|
spring.mvc.formcontent.putfilter.enabled = false |
org.springframework.web.filter.hiddenhttpmethodfilter
引入版本
org.springframework.web.filter.hiddenhttpmethodfilter
由 springframework 3.0 版本引入,分發在org.springframework:spring-web
中。
使用場景
當 web 服務端同一資源(url)提供了多請求方法的實現,例如 uri :/update 提供了http post 以及 http put 實現),通常 web 場景中,瀏覽器僅支持 http get或者 post 請求方法,這樣的話,瀏覽器無法發起 http put 請求。
為了瀏覽器可以消費 http put 資源, 需要在服務端將 http post 轉化成http put 請求,為了解決這類問題,spring 引入org.springframework.web.filter.hiddenhttpmethodfilter web
組件。
當瀏覽器 發起 http post 請求時,可通過增加請求參數(默認參數名稱:"_method")的方式,進行http 請求方法切換,
org.springframework.web.filter.hiddenhttpmethodfilter
獲取參數"_method"值后,將參數值作為httpservletrequest#getmethod()
的返回值,給后續 servlet實現使用。
出于通用性的考慮,org.springframework.web.filter.hiddenhttpmethodfilter
通過調用 #setmethodparam(string)
方法,來修改轉換請求方法的參數名稱。
spring boot 默認場景下,將org.springframework.web.filter.httpputformcontentfilter
被org.springframework.boot.autoconfigure.web.webmvcautoconfiguration
自動裝配,以下為 spring boot 1.4.1.release 以及更好版本定義(可能存在一定的差異):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@configuration @conditionalonwebapplication @conditionalonclass ({ servlet. class , dispatcherservlet. class , webmvcconfigureradapter. class }) @conditionalonmissingbean (webmvcconfigurationsupport. class ) @autoconfigureorder (ordered.highest_precedence + 10 ) @autoconfigureafter ({ dispatcherservletautoconfiguration. class , validationautoconfiguration. class }) public class webmvcautoconfiguration { ...... @bean @conditionalonmissingbean (hiddenhttpmethodfilter. class ) public orderedhiddenhttpmethodfilter hiddenhttpmethodfilter() { return new orderedhiddenhttpmethodfilter(); } ...... } |
綜上所述,org.springframework.web.filter.hiddenhttpmethodfilter 也是特殊場景下所需,故可以關閉之。
配置項
按目前最新的 spring boot 1.5.2.release 版本中實現,也沒有提供類似spring.mvc.formcontent.putfilter.enabled 這樣的配置項關閉,無法關閉。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000015742857