Spring Boot?v1.4.0.RELEASE
Spring Boot 1.4.0 Release Notes
Upgrading from Spring Boot 1.3
Deprecations from Spring Boot 1.3
在Spring Boot 1.3中棄用的類、方法和屬性已經在這個版本中刪除了。請確保在升級之前沒有調用已棄用的方法。
在?Apache EOL announcement?后已經刪除了對Log4j 1 的支持
Renamed starters
以下啟動器已被重命名,舊的啟動器將在Spring Boot 1.5中刪除
spring-boot-starter-ws?→?spring-boot-starter-web-services
spring-boot-starter-redis?→?spring-boot-starter-data-redis
DataSourceProperties get methods
DataSourceProperties中的一些get…方法已被更改,以與其他@ConfigurationProperties類更加一致。如果你之前在你的代碼中直接調用了以下任何一個方法,你需要遷移到新的determine…()等號:
getDriverClassName()?→?determineDriverClassName()
getUrl()?→?determineUrl()
getUsername()?→?determineUsername()
getPassword()?→?determineUsername()
備注:get方法沒有被棄用,但它們的行為已經改變,請確保在升級時手動檢查是否使用。
DataSource binding
Prior to Spring Boot 1.4, auto-configured datasources were bound to the?spring.datasource?namespace. In 1.4, we only bind the common settings to?spring.datasource?(see?DataSourceProperties) and we have defined newspecificnamespaces for the four connections pools we support (in that order):
在Spring Boot 1.4之前,自動配置的數據源被綁定到spring.datasource名稱空間。在1.4中,我們只將公共設置綁定到spring.datasource(參見DataSourceProperties),我們已經為我們支持的四個連接池定義了新的明確的命名空間(按此順序):
spring.datasource.tomcat?for?org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.hikari?for?com.zaxxer.hikari.HikariDataSource
spring.datasource.dbcp?for?org.apache.commons.dbcp.BasicDataSource
spring.datasource.dbcp2?for?org.apache.commons.dbcp2.BasicDataSource
如果使用正在使用的連接池實現的特定設置,則必須將該配置移動到相關的名稱空間。例如,如果您正在使用Tomcat的testOnBorrow標志,則必須將其從spring.datasource.test-on-borrow移走到spring.datasource.tomcat.test-on-borrow。
如果在IDE中使用配置幫助,現在可以看到每個連接池都有哪些設置可用,而不是在spring.datasource 名稱空間中將所有設置混合在一起。這將使您更容易地確定什么實現支持什么特性。
JTA settings binding
與數據源綁定類似,Atomikos和Bitronix的JTA提供程序特定配置屬性也被綁定到spring.jta。它們現在分別被綁定到spring.jta.atomikos.properties和spring.jta.bitronix.properties;這些條目的元數據也得到了極大的改進。
Hibernate 5
Hibernate 5.0現在被用作默認的JPA持久性提供程序。如果您從Spring Boot 1.3升級,那么您將從Hibernate 4.3升級到Hibernate 5.0。請參考Hibernate遷移文檔-Hibernate migration documentation?了解一般的升級說明。此外,你應該知道以下幾點:
Naming Strategy
SpringNamingStrategy不再使用,因為Hibernate 5.1刪除了對舊的NamingStrategy接口的支持。一個新的SpringPhysicalNamingStrategy現在是自動配置的,它與Hibernate的默認ImplicitNamingStrategy一起使用。這應該與Spring Boot 1.3默認值非常接近(如果不是完全相同的話),但是,在升級時應該檢查數據庫模式是否正確。
如果您在升級之前已經在使用Hibernate 5,那么您可能正在使用Hibernate 5的默認值。如果您想在升級后恢復它們,請在配置中設置此屬性:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Generator mappings
為了減少升級的痛苦,我們對于Hibernate設置了hibernate.id.new_generator_mappings=false。Hibernate團隊通常不推薦這種設置,所以如果您愿意處理生成器更改,您可能希望在application.properties文件中將spring.jpa.hibernate.use-new-id-generator-mappings設置為true。
Downgrading to Hibernate 4.3
如果你在升級到Hibernate 5.0時遇到了大問題,你可以在你的pom.xml中通過重?hibernate.version屬性來降級到舊的Hibernate版本:
或者在你的Gradle腳本重寫?hibernate.version 屬性:
備注:在Spring Boot 1.4之后,Hibernate 4.3將不受支持。如果你發現一個阻止你升級的bug-raise an issue,請提出一個問題。
@EntityScan
@org.springframework.boot.orm.jpa.EntityScan注解已經棄用,應該用@org.springframework.boot.autoconfigure.domain.EntityScan替換或顯式配置。
例如,如果您有以下配置:
如果您使用的是自動配置LocalContainerEntityManagerFactoryBean, 切換到:
或者如果你正在定義你自己的LocalContainerEntityManagerFactoryBean,完全刪除@EntityScan注解,調用?LocalContainerEntityManagerFactoryBean.setPackagesToScan(…?)?或者使用EntityManagerFactoryBuilder packages(…)方法:
Test utilities and classes
Spring?Boot 1.4附帶了一個新的spring-boot-test?模塊,它包含了一個完全重組的org.springframework.boot.test包。升級Spring Boot 1.3應用程序時,應該從舊包中已棄用的類遷移到新結構中的等價類。如果你正在使用Linux或OSX,你可以使用以下命令遷移代碼:
find . -type f -name '*.java' -exec sed -i '' \
-e s/org.springframework.boot.test.ConfigFileApplicationContextInitializer/org.springframework.boot.test.context.ConfigFileApplicationContextInitializer/g \
-e s/org.springframework.boot.test.EnvironmentTestUtils/org.springframework.boot.test.util.EnvironmentTestUtils/g \
-e s/org.springframework.boot.test.OutputCapture/org.springframework.boot.test.rule.OutputCapture/g \
-e s/org.springframework.boot.test.SpringApplicationContextLoader/org.springframework.boot.test.context.SpringApplicationContextLoader/g \
-e s/org.springframework.boot.test.SpringBootMockServletContext/org.springframework.boot.test.mock.web.SpringBootMockServletContext/g \
-e s/org.springframework.boot.test.TestRestTemplate/org.springframework.boot.test.web.client.TestRestTemplate/g \
{} \;
此外,Spring Boot 1.4試圖合理化和簡化Spring Boot測試可以運行的各種方法。您應該遷移以下內容以使用新的@SpringBootTest注解:
從 @SpringApplicationConfiguration(classes=MyConfig.class)?到 @SpringBootTest(classes=MyConfig.class)
從 @ContextConfiguration(classes=MyConfig.class, loader=SpringApplicationContextLoader.class)?到 @SpringBootTest(classes=MyConfig.class)
從 @IntegrationTest?到 @SpringBootTest(webEnvironment=WebEnvironment.NONE)
從 @IntegrationTest with @WebAppConfiguration?到 @SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)?(or?RANDOM_PORT)
從 @WebIntegrationTest?到 @SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)?(or?RANDOM_PORT)
提示:在遷移測試時,您可能還想用Spring 4.3中可讀性更強的@RunWith(SpringJUnit4ClassRunner.class)來替換任何@RunWith(SpringJUnit4ClassRunner.class)聲明。
For more details on the?@SpringBootTest?annotation refer to the?updated documentation.
TestRestTemplate
TestRestTemplate?類不再直接擴展RestTemplate(盡管它繼續提供相同的方法)。這允許將TestRestTemplate配置為bean,而不會意外地注入它。如果需要訪問實際的底層RestTemplate,請使用getRestTemplate()方法。
Maven spring-boot.version property
spring-boot.version?屬性已經從 spring-boot-dependencies?pom.刪除。See?issue 5104?for details.
Integration Starter
spring-boot-starter-integration通過刪除四個典型Spring Integration?應用程序不需要使用的模塊而得到了簡化。被刪除的四個模塊是:
spring-integration-file
spring-integration-http
spring-integration-ip
spring-integration-stream
如果你的應用程序依賴于這四個模塊中的任何一個,你應該給你的pom或build.gradle添加一個顯式的依賴項。
此外,spring-integration-java-dsl和spring-integration-jmx現在已經添加到starter中。使用DSL是在應用程序中配置Spring Integration的推薦方法。
Spring Batch Starter
spring-boot-starter-batch starter不再依賴于嵌入式數據庫。如果您依賴于這種安排,請添加您選擇的數據庫(驅動程序),例如:
如果您已經配置了自己的hsqldb上的排除,那么現在可以刪除該排除。
Downgrading Tomcat
從Tomcat 8.5.4開始,?tomcat-juli模塊現在被打包為嵌入式Tomcat的一部分。大多數用戶不會注意到這個更改,但是,如果您手動降級到較老的Tomcat版本,那么您現在需要自己添加tomcat-juli模塊。有關更新的說明,請參閱如何使用文檔部分-how-to documentation section?。
Dispatch Options Request
默認spring.mvc.dispatch-options-request?屬性已經從false改為true,以符合Spring框架的首選默認值。如果您不希望OPTIONS請求被分派到FrameworkServlet。你應該顯式地設置?spring.mvc.dispatch-options-request?為false。
Forced character encoding
強制字符編碼現在只適用于請求(而不是響應)。如果您想強制對請求和響應進行編碼,可以設置spring.http.encoding.force=true。
multipart屬性已經從multipart.?命名空間移動到spring.http.multipart. 命名空間。
Server header
Server?HTTP響應頭不再被設置,除非server.server-header屬性被設置。
@ConfigurationProperties default bean names
當通過@EnableConfigurationProperties(SomeBean.class)注冊@ConfigurationProperties bean時,我們使用<prefix>.CONFIGURATION_PROPERTIES形式生成一個bean名。從Spring Boot 1.4開始,我們已經更改了該模式,以避免兩個bean使用相同前綴時發生名稱沖突。
新的常規名稱是<prefix>-<fqn>,其中<prefix>是@ConfigurationProperties注解中指定的環境鍵前綴,也是bean的完全限定名。如果注解不提供任何前綴,則只使用bean的完全限定名。
Jetty JNDI support
spring-boot-starter-jetty“Starter”不再包括org.eclipse.jetty:jetty-jndi。如果您使用具有JNDI的Jetty,您現在需要自己直接添加這個依賴項。
Guava caches
建議使用Guava緩存支持的開發人員遷移到Caffeine。
Remote Shell
CRaSH屬性已經從shell.?命名空間移動到management.shell.?名稱空間。另外,身份驗證類型現在應該通過management.shell.auth.type來定義。
Spring Session auto-configuration improvements
Spring Boot支持更多的Spring Session后端存儲:除了Redis, JDBC, MongoDB, Hazelcast和內存并發哈希映射也支持。一個新的spring.session.store-type?store-type強制屬性引入了來選擇應該使用的存儲 Spring Session。
Launch script identity
當啟動腳本確定應用程序的默認標識時,現在將使用包含jar的目錄的規范名稱。以前,如果包含jar的目錄是一個符號鏈接,則使用符號鏈接的名稱。如果需要對應用程序的標識進行更多的控制,則應該使用APP_NAME環境變量。
MongoDB 3
Mongo的Java驅動程序的默認版本現在是3.2.2(從2.14.2),spring-boot-starter-data-mongodb已經更新為使用新的首選mongodb-driver工件。
嵌入式MongoDB的自動配置也被更新為使用3.2.2作為默認版本。
Thymeleaf 3
默認情況下,Spring Boot使用的是Thymeleaf 2.1,但是現在它也兼容Thymeleaf 3,更多細節請查看更新后的文檔-updated documentation。
Executable jar layout
可執行jar的布局已經改變。如果您正在使用Spring Boot的Maven、Gradle或Ant支持來構建應用程序,那么此更改不會影響您。如果你自己構建一個可執行的存檔文件,請注意,應用程序的依賴項現在打包在BOOT-INF/lib中,而不是lib中,應用程序自己的類現在打包在BOOT-INF/classes中,而不是jar的根目錄中。
Jersey classpath scanning limitations
對可執行jar布局的更改意味著Jersey的類路徑掃描的限制(?limitation in Jersey’s classpath scanning?)現在會影響可執行jar文件和可執行war文件。為了解決這個問題,您希望Jersey掃描的類應該打包在一個jar中,并作為依賴項包含在BOOT-INF/lib中。Spring Boot啟動器應該配置為在啟動時解包這些jar(configured to unpack those jars on start up?),以便Jersey可以掃描它們的內容。
Integration tests with the?maven-failsafe-plugin
從Failsafe 2.19開始,target/classes不再在類路徑上,而是使用項目的構建jar。由于可執行jar布局的改變,插件將無法找到你的類。有兩種方法可以解決這個問題:
1. 降級到2.18.1,這樣你就可以使用target/classes了
2. 配置spring-boot-maven-plugin來使用aclassifier來實現重新打包的目標。這樣,原始的jar就可以被插件使用了。例如
備注:如果您正在使用Spring Boot的依賴項管理,那么沒有什么可做的,因為默認情況下您將使用2.18.1。
Tip Watch?SUREFIRE-1198?for updates on this issue.
HornetQ
對HornetQ的支持已被棄用。HornetQ的用戶應該考慮遷移到Artemis。
@Transactional?default to cglib proxies
當啟動自動配置事務管理時,proxyTargetClass現在被設置為true(這意味著創建了cglib代理,而不是要求bean實現接口)。如果你想對其他沒有自動配置的方面的行為進行對齊,你需要現在顯式啟用該屬性:
@EnableCaching(proxyTargetClass=true)
備注:如果您碰巧在接口上使用了@Transactional,則必須顯式地將@EnableTransactionManagement添加到配置中。這將恢復以前的行為。
New and Noteworthy
Tip Check?the configuration changelog?for a complete overview of the changes in configuration.
Spring Framework 4.3
Spring Boot 1.4 builds on and requires Spring Framework 4.3. There are a number of nice refinements in Spring Framework 4.3 including new Spring MVC?@RequestMapping?annotations. Refer to theSpring Framework reference documentationfor details.
Spring Boot 1.4構建于Spring Framework 4.3之上,并且需要Spring Framework 4.3。在Spring Framework 4.3中有許多出色的改進,包括新的Spring MVC @RequestMapping注釋。詳細信息請參考Spring Framework reference documentation。
注意,Spring framework 4.3中的測試框架需要JUnit 4.12。See?SPR-13275?for further details.
Third-party library upgrades
許多第三方庫已經升級到最新版本。更新包括Jetty 9.3, Tomcat 8.5, Jersey 2.23, Hibernate 5.0, Jackson 2.7, Solr 5.5, Spring Data Hopper, Spring Session 1.2, Hazelcast 3.6, Artemis 1.3, Ehcache 3.1, Elasticsearch 2.3, Spring REST Docs 1.1, Spring AMQP 1.6 & Spring Integration 4.3。
幾個Maven插件也進行了升級。
Couchbase support
現在為Couchbase提供了完全的自動配置支持。通過添加spring-boot-starter-data-couchbase“Starter”并提供一些配置,你可以很容易地訪問Bucket和Cluster?bean:
spring.couchbase.bootstrap-hosts=my-host-1,192.168.1.123
spring.couchbase.bucket.name=my-bucket
spring.couchbase.bucket.password=secret
還可以使用Couchbase作為Spring Data?Repository?的后備存儲或CacheManager。Refer to the?updated documentation?for details。
Neo4J Support
Neo4J現在提供了自動配置支持。您可以連接到遠程服務器或運行嵌入式Neo4J服務器。你也可以使用Neo4J來支持Spring Data?Repository,例如:
Full details are provided in the?Neo4J section?of the reference documentation.
Redis Spring Data repositories
Redis現在可以用來支持Spring數據存儲庫。See the?Spring Data Redis?documentation for more details.
Narayana transaction manager support
自動配置支持現在包含在Narayana事務管理器中。如果您需要JTA支持,您可以選擇Narayana, Bitronix或Atomkos。See?the updated reference guide?for details。
Caffeine cache support
Caffeine?v2.2 (Java 8重寫了Guava的緩存支持)提供了自動配置。現有的Guava?緩存用戶應該考慮遷移到Caffeine?,因為Guava?緩存支持將在未來的版本中刪除。
Elasticsearch Jest support
如果Jest在類路徑上,Spring Boot會自動配置JestClient和專用的HealthIndicator。即使spring-data-elasticsearch不在類路徑上,也可以使用Elasticsearch。
Analysis of startup failures
Spring Boot現在將對常見的啟動失敗進行分析,并提供有用的診斷信息,而不僅僅是記錄異常及其堆棧跟蹤。例如,在Spring Boot的早期版本中,由于嵌入式servlet容器的端口正在使用而導致的啟動失敗如下所示:
2016-02-16 17:46:14.334 ERROR 24753 --- [? ? ? ? ? main] o.s.boot.SpringApplication? ? ? ? ? ? ? : Application startup failed
java.lang.RuntimeException: java.net.BindException: Address already in use
? ? at io.undertow.Undertow.start(Undertow.java:181) ~[undertow-core-1.3.14.Final.jar:1.3.14.Final]
? ? at org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer.start(UndertowEmbeddedServletContainer.java:121) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
? ? at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
? ? at sample.undertow.SampleUndertowApplication.main(SampleUndertowApplication.java:26) [classes/:na]
Caused by: java.net.BindException: Address already in use
? ? at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_60]
? ? at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_60]
? ? at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_60]
? ? at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_60]
? ? at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_60]
? ? at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:190) ~[xnio-nio-3.3.4.Final.jar:3.3.4.Final]
? ? at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:243) ~[xnio-api-3.3.4.Final.jar:3.3.4.Final]
? ? at io.undertow.Undertow.start(Undertow.java:137) ~[undertow-core-1.3.14.Final.jar:1.3.14.Final]
? ? ... 11 common frames omitted
在1.4版本中,它看起來是這樣的:
2016-02-16 17:44:49.179 ERROR 24745 --- [? ? ? ? ? main] o.s.b.d.LoggingFailureAnalysisReporter? :
***************************
APPLICATION FAILED TO START
***************************
Description:
Embedded servlet container failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
如果您仍然想要查看底層原因的堆棧跟蹤,可以為org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter啟用調試日志記錄。
Image Banners
你現在可以使用圖像文件來渲染ASCII藝術橫幅。一個banner.gif, banner.jpg或banner.png文件到src/main/resources,使其自動轉換為ASCII。你可以使用banner.image.width和banner.image.height屬性來調整大小,或使用banner.image.invert顛倒顏色。
RestTemplate builder
可以使用新的RestTemplateBuilder輕松創建具有合理默認值的RestTemplate。默認情況下,構建的RestTemplate將嘗試使用類路徑上可用的最合適的ClientHttpRequestFactory,并將知道要使用的MessageConverter實例(包括Jackson)。構建器包括許多有用的方法,可用于快速配置RestTemplate。例如,要添加基本的認證支持,你可以使用:
自動配置的TestRestTemplate現在也使用RestTemplateBuilder。
JSON Components
現在為自定義Jackson JsonSerializer和 /or?JsonDeserializer?注冊提供了一個新的@JsonComponent注解。這是一種解耦JSON序列化邏輯的有用方法:
此外,Spring Boot現在還提供了JsonObjectSerializer和JsonObjectDeserializer基類,在序列化對象時提供了標準Jackson版本的有用替代方案。?See the?updated documentation?for details。
Convention based error pages
現在可以通過遵循基于約定的方法創建給定狀態代碼的自定義錯誤頁面。在/public/error中創建一個靜態HTML文件,或者使用狀態代碼作為文件名在/templates/error中添加一個模板。例如,要注冊一個自定義404文件,可以添加src/main/resource/public/error/404.html。See?the updated reference documentation?for details。
Unified?@EntityScan?annotation
org.springframework.boot.autoconfigure.domain.EntityScan現在可以用來指定用于JPA、Neo4J、MongoDB、Cassandra和Couchbase的包。因此,JPA特有的org.springframework.boot.orm.jpa.EntityScan現在已棄用。
ErrorPageRegistry
新的ErrorPageRegistry和ErrorPageRegistrar接口允許以一致的方式注冊錯誤頁面,而不管是否使用嵌入式servlet容器。ErrorPageFilter類已經更新為ErrorPageRegistry,而不是一個虛假的ConfigurableEmbeddedServletContainer。
PrincipalExtractor
如果需要使用自定義邏輯提取OAuth2主體,現在可以使用PrincipalExtractor接口。
Test improvements
Spring Boot 1.4對測試支持進行了重大的修改。現在在專用的spring-boot-test和spring-boot-test-autoconfigure jar中提供了測試類和實用程序(盡管大多數用戶將繼續通過spring-boot-starter-test“Starter”來獲取它們)。我們已經向測試啟動器添加了AssertJ、JSONassert和JsonPath依賴項。
@SpringBootTest
在Spring Boot 1.3中,有多種編寫Spring Boot測試的方法。您可以使用@SpringApplicationConfiguration、@ContextConfiguration和SpringApplicationContextLoader、@IntegrationTest或@WebIntegrationTest。在SpringBoot 1.4中,一個@SpringBootTest注解可以替換所有這些。
結合使用@SpringBootTest和@RunWith(SpringRunner.class),并根據您想要編寫的測試類型設置webEnvironment屬性。
一個經典的集成測試,使用一個模擬的servlet環境:
一個web集成測試,運行一個實時服務器在一個定義的端口上監聽:
一個web集成測試,運行一個實時服務器在一個隨機端口上監聽:
See the?updated reference documentation?for details.
Auto-detection of test configuration
現在可以為大多數測試自動檢測測試配置。如果您遵循Spring Boot為構建代碼而推薦的約定,則在沒有定義顯式配置時將加載@SpringBootApplication類。如果您需要加載不同的@Configuration類,您可以將其作為嵌套的內部類包含在您的測試中,或者使用@SpringBootTest的classes屬性。
See?Detecting test configuration?for details.
Mocking and spying beans
為了測試目的,通常希望用mock替換ApplicationContext中的單個bean。在Spring Boot 1.4中,這就像用@MockBean在測試中注解一個字段一樣簡單:
如果您想監視現有bean而不是使用完整的mock,也可以使用@SpyBean。
See the?mocking section?of the reference documentation for more details.
Auto-configured tests
對測試來說,完全的應用程序自動配置有時是多余的,您通常只想自動配置應用程序的特定“部分”。Spring Boot 1.4引入了一些專門的測試注解,可以用來測試應用程序的特定部分:
@JsonTest?- 測試JSON編組和解組。
@WebMvcTest?- 使用MockMVC測試Spring MVC @Controllers。
@RestClientTest?- 用于測試RestTemplate調用。
@DataJpaTest?- 用于測試Spring Data JPA元素
許多注解提供了特定于測試的附加自動配置。例如,如果你使用@WebMvcTest,你可以@Autowire一個完全配置的MockMvc實例。
See the?reference documentation?for details.
JSON AssertJ assertions
新的JacksonTester、GsonTester和BasicJsonTester類可以與AssertJ結合使用來測試JSON編組和解組。測試人員可以與@JsonTest注解一起使用,也可以直接在測試類中使用:
See the?JSON section?of the reference documentation or the Javadocs for details.
@RestClientTest
如果您想測試REST客戶端,可以使用@RestClientTest注解。默認情況下,它將自動配置Jackson和GSON支持,配置RestTemplateBuilder并添加對MockRestServiceServer的支持。
Auto-configuration for Spring REST Docs
結合上述對自動配置MockMvc的支持,Spring REST Docs的自動配置已經被引入。可以使用新的@AutoConfigureRestDocs注解啟用REST Docs。這將導致MockMvc實例被自動配置為使用REST Docs,并且不再需要使用REST Docs的JUnit規則。詳情請參閱參考文檔的相關部分-relevant section。
Test utilities
spring-boot-starter-test 現在提供?AssertJ?assertions library.
測試實用程序從org.springframework.boot.test包中已經轉移到spring-boot-test專用工件。
Actuator info endpoint improvements
現在,您可以使用InfoContributor接口注冊向/info執行器端點公開信息的bean。提供開箱支持:
從?git-commit-id-plugin?Maven或gradle-git-properties?Gradle 插件中生成的全部或部分Git信息(設置?management.info.git.mode=full?以顯示全部細節)
從Spring Boot Maven或Gradle插件生成的構建信息。
自定義環境信息(任何屬性starting?info.*)
Details are documented in the?"Application information"?section of the reference docs.
MetricsFilter improvements
MetricsFilter現在可以以經典的“merged”形式提交度量,或者按每個HTTP方法分組。使用endpoints.metrics.filter屬性以更改配置:
endpoints.metrics.filter.gauge-submissions=per-http-method
endpoints.metrics.filter.counter-submissions=per-http-method,merged
Spring Session JDBC Initializer
如果將Spring Session配置為使用JDBC存儲,那么模式將在啟動時自動創建。
Secured connection for Artemis/HornetQ
Spring Boot現在允許通過一個安全的Artemis/HornetQ代理進行連接。
Annotation processing
Apache HttpCore 4.4.5刪除了一些注解-?removed a handful of annotations。如果您正在使用注解處理器,并且正在對使用刪除的注解之一的類進行子類化,那么這是一個二進制不兼容的更改。例如,如果類使用了@Immutable,你會看到編譯時注解處理失敗,并出現[ERROR]診斷:ERROR: cannot access org.apache.http.annotation.Immutable。
這個問題可以通過降級到HttpCore 4.4.4來避免,或者更好的是,通過結構化你的代碼,這樣有問題的類就不會受到編譯時注釋處理的影響。
Miscellaneous
server.jetty.acceptors和server.jetty.selectors屬性已經添加了來配置Jetty接受器和選擇器的數量
server.max-http-header-size?和 server.max-http-post-size可以被用來約束 HTTP headers 和 HTTP POSTs最大數量。 設置工作在Tomcat, Jetty和Undertow
Tomcat的最小空閑線程數量現在可以使用server.tomcat.min-spare-threads配置
現在可以在application.yml文件使用配置文件否定。在?spring.profile values中使用熟悉的!?prefix
執行器公開一個新的headdump端點,該端點返回一個GZip壓縮的hprof堆轉儲文件
Spring Mobile現在可以為所有支持的模板引擎自動配置
Spring Boot maven插件允許使用新的includeSystemScope屬性將系統范圍內的工件打包
spring.mvc.log-resolved-exception啟用在HandlerExceptionResolver解決異常時自動記錄警告
spring.data.cassandra.schema-action用于自定義啟動時的模式操作
Spring Boot的fat jar格式現在應該消耗更少的內存
現在通過spring.http.encoding.mapping.<locale>=<charset> 屬性支持區域設置到字符集的映射。
默認情況下,使用spring.mvc.locale?的區域設置現在被請求的Accept-Language標頭覆蓋。要恢復1.3中頭文件被忽略的行為,設置spring.mvc.locale-resolve固定。
Deprecations in Spring Boot 1.4
對Velocity的支持已經被棄用,因為它在Spring Framework 4.3中已經被棄用了
UndertowEmbeddedServletContainer中的一些構造函數已經棄用(大多數使用應該不受影響)。
@ConfigurationProperties注解的位置和合并屬性已經棄用,而是支持直接配置環境。
保護的SpringApplication.printBanner方法不應再用于打印自定義橫幅。使用Banner接口代替。
保護的InfoEndpoint.getAdditionalInfo方法已被棄用,取而代之的是InfoContributor接口。
org.springframework.boot.autoconfigure.test.ImportAutoConfiguration?已經移動到 org.springframework.boot.autoconfigure.
?org.springframework.boot.test 包下的所有類已經廢棄。 See the "upgrading" notes above for details.
PropertiesConfigurationFactory.setProperties(Properties)?被廢棄,取而代之的是使用PropertySources.
org.springframework.boot.context.embedded 包下的幾個類被廢棄 并且重新安置在org.springframework.boot.web.servlet.
org.springframework.boot.context.web包下的所有類被廢棄并且被重新安置
spring-boot-starter-ws?"Starter" 已經重命名為spring-boot-starter-web-services.
spring-boot-starter-redis?"Starter" 已經重命名為spring-boot-starter-data-redis.
spring-boot-starter-hornetq?starter 和 auto-configuration已經被廢棄,取而代之的是spring-boot-starter-artemis
management.security.role已經被廢棄,取而代之的是management.security.roles
@org.springframework.boot.orm.jpa.EntityScan?注解已經被廢棄,取而代之的是@org.springframework.boot.autoconfigure.domain.EntityScan?或 顯示配置
TomcatEmbeddedServletContainerFactory.getValves()?已經被廢棄,取而代之的是getContextValves().
org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter?已經被廢棄,取而代之的是org.springframework.boot.system.EmbeddedServerPortFileWriter
org.springframework.boot.actuate.system.ApplicationPidFileWriter已經被廢棄,取而代之的是org.springframework.boot.system.ApplicationPidFileWriter
Property Renames
spring.jackson.serialization-inclusion?應該被 spring.jackson.default-property-inclusion替代
spring.activemq.pooled?should 應該被 spring.activemq.pool.enabled替代
spring.jpa.hibernate.naming-strategy應該被spring.jpa.hibernate.naming.strategy替代
server.tomcat.max-http-header-size?應該被 server.max-http-header-size替代