1. 简介
本篇文章将梳理 Spring 和 Spring Boot 中从 4.x 和 1.4 版本以来被废弃的类,并说明这些类被哪些新类替代。这对于项目升级、维护老系统、以及保持代码现代化非常有帮助。
我们将按 Spring 版本划分,列出废弃类及其替代方案,并附上相关说明。面向读者为有一定经验的 Java 开发者,内容简洁、直奔主题。
2. Spring 中的废弃类
为了方便查阅,我们将废弃类按 Spring 的发布版本进行分类,并在每个版本下按类名排序(忽略包名)。
2.1. Spring 4.0.x
- ✅
org.springframework.cache.interceptor.DefaultKeyGenerator
→ 替换为SimpleKeyGenerator
或自定义KeyGenerator
- ✅
org.springframework.jdbc.support.lob.OracleLobHandler
→ 使用DefaultLobHandler
(适用于 Oracle 10g 及以上) - ✅
org.springframework.test.AssertThrows
→ 使用 JUnit 4 的@Test(expected=...)
- ✅
org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter
→ 替换为AllEncompassingFormHttpMessageConverter
⚠️ 以下类自 Spring 4.0.2 起废弃,Spring 4.1 中移除:
org.springframework.cglib.transform.impl.MemorySafeUndeclaredThrowableStrategy
→ 因 CGLIB 3.1 默认策略变更
2.2. Spring 4.1.x
- ✅
org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper
→BeanPropertyRowMapper
- ✅
org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper
→SingleColumnRowMapper
2.3. Spring 4.2.x
- ✅
org.springframework.web.servlet.view.document.AbstractExcelView
→AbstractXlsView
及其子类AbstractXlsxView
、AbstractXlsxStreamingView
- ✅
org.springframework.format.number.CurrencyFormatter
→CurrencyStyleFormatter
- ✅
org.springframework.messaging.simp.user.DefaultUserSessionRegistry
→ 使用SimpUserRegistry
+ApplicationListener<AbstractSubProtocolEvent>
- ✅
org.springframework.messaging.handler.HandlerMethodSelector
→ 使用MethodIntrospector
- ✅
org.springframework.core.JdkVersion
→ 直接通过反射判断 JDK 版本 - ✅
org.springframework.format.number.NumberFormatter
→NumberStyleFormatter
- ✅
org.springframework.format.number.PercentFormatter
→PercentStyleFormatter
- ✅
org.springframework.test.context.transaction.TransactionConfigurationAttributes
→ 与@TransactionConfiguration
一同在 Spring 5 中移除 - ✅
org.springframework.oxm.xmlbeans.XmlBeansMarshaller
→ Apache XMLBeans 已停止维护
📌 Apache Log4j 1.x 相关类废弃,建议迁移到 Log4j 2
org.springframework.web.util.Log4jConfigListener
org.springframework.util.Log4jConfigurer
org.springframework.web.filter.Log4jNestedDiagnosticContextFilter
org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor
org.springframework.web.util.Log4jWebConfigurer
2.4. Spring 4.3.x
Spring 4.3 是一个大版本,废弃类较多,这里按功能分类:
常规替换类
- ✅
org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice
→ Spring 5.1 中移除,建议使用 CORS - ✅
org.springframework.oxm.castor.CastorMarshaller
→ Castor 项目已停止维护 - ✅
org.springframework.web.servlet.mvc.method.annotation.CompletionStageReturnValueHandler
→ 使用DeferredResultMethodReturnValueHandler
- ✅
org.springframework.jdbc.support.incrementer.DB2MainframeSequenceMaxValueIncrementer
→ 重命名为Db2MainframeMaxValueIncrementer
- ✅
org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer
→ 重命名为Db2LuwMaxValueIncrementer
- ✅
org.springframework.core.GenericCollectionTypeResolver
→ 使用ResolvableType
- ✅
org.springframework.web.servlet.mvc.method.annotation.ListenableFutureReturnValueHandler
→ 同上,使用DeferredResultMethodReturnValueHandler
- ✅
org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer
→ 使用PostgresSequenceMaxValueIncrementer
- ✅
org.springframework.web.servlet.ResourceServlet
→ 使用ResourceHttpRequestHandler
MVC 基于 HandlerMethod
的基础设施替代类
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
org.springframework.web.bind.annotation.support.HandlerMethodInvoker
org.springframework.web.bind.annotation.support.HandlerMethodResolver
注解驱动替代类
org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping
org.springframework.web.servlet.mvc.multiaction.AbstractUrlMethodNameResolver
org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping
org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver
org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver
org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver
Hibernate 3 替代类(建议升级到 Hibernate 4.x/5.x)
org.springframework.orm.hibernate3.*
系列类全部废弃
Velocity 相关废弃类(Spring 4.3 中 Velocity 支持废弃)
org.springframework.web.servlet.view.velocity.*
全部废弃
SockJS 中 JSONP 相关类(Spring 5.1 移除)
org.springframework.web.socket.sockjs.transport.handler.JsonpPollingTransportHandler
org.springframework.web.socket.sockjs.transport.handler.JsonpReceivingTransportHandler
无替代类
org.springframework.core.ControlFlowFactory
org.springframework.util.WeakReferenceMonitor
2.5. Spring 5.0.x
- ✅
org.springframework.web.reactive.support.AbstractAnnotationConfigDispatcherHandlerInitializer
→ 使用AbstractReactiveWebInitializer
- ✅
org.springframework.web.util.AbstractUriTemplateHandler
→DefaultUriBuilderFactory
- ✅
org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer
→ 使用WebSocketMessageBrokerConfigurer
- ✅
org.springframework.web.client.AsyncRestTemplate
→ 使用WebClient
- ✅
org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter
→CallableProcessingInterceptor
已有默认方法 - ✅
org.springframework.messaging.support.ChannelInterceptorAdapter
→ChannelInterceptor
已有默认方法 - ✅
org.springframework.util.comparator.CompoundComparator
→ 使用 JDK 8 的Comparator.thenComparing
- ✅
org.springframework.web.util.DefaultUriTemplateHandler
→DefaultUriBuilderFactory
(注意parsePath
默认值变更) - ✅
org.springframework.web.context.request.async.DeferredResultProcessingInterceptorAdapter
→DeferredResultProcessingInterceptor
已有默认方法 - ✅
org.springframework.util.comparator.InvertibleComparator
→ 使用 JDK 8 的Comparator.reversed()
- ✅
org.springframework.http.client.Netty4ClientHttpRequestFactory
→ 使用ReactorClientHttpConnector
- ✅
org.apache.commons.logging.impl.SimpleLog
→ 移至spring-jcl
(等价于NoOpLog
) - ✅
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
→WebMvcConfigurer
已有默认方法 - ✅
org.springframework.beans.factory.config.YamlProcessor.StrictMapAppenderConstructor
→ SnakeYAML 自带重复键处理
替换为 AbstractReactiveWebInitializer
org.springframework.web.reactive.support.AbstractDispatcherHandlerInitializer
org.springframework.web.reactive.support.AbstractServletHttpHandlerAdapterInitializer
无替代类
org.springframework.http.client.support.AsyncHttpAccessor
org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory
org.springframework.http.client.InterceptingAsyncClientHttpRequestFactory
org.springframework.http.client.support.InterceptingAsyncHttpAccessor
org.springframework.mock.http.client.MockAsyncClientHttpRequest
2.6. Spring 5.1.x
- ✅
org.springframework.http.client.support.BasicAuthorizationInterceptor
→ 使用BasicAuthenticationInterceptor
- ✅
org.springframework.jdbc.core.BatchUpdateUtils
→ 不再被JdbcTemplate
使用 - ✅
org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials
→ 构建请求时使用HttpHeaders.setBasicAuth
- ✅
org.springframework.web.filter.reactive.ForwardedHeaderFilter
→ 使用ForwardedHeaderTransformer
- ✅
org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils
→ 不再被NamedParameterJdbcTemplate
使用 - ✅
org.springframework.core.io.PathResource
→ 使用FileSystemResource(Path)
- ✅
org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
→ 使用构造函数注入 - ✅
org.springframework.remoting.caucho.SimpleHessianServiceExporter
→ 使用HessianServiceExporter
- ✅
org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter
→ 使用HttpInvokerServiceExporter
- ✅
org.springframework.remoting.support.SimpleHttpServerFactoryBean
→ 使用嵌入式 Tomcat/Jetty/Undertow - ✅
org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter
→ 使用SimpleJaxWsServiceExporter
替换为 EncodedResourceResolver
org.springframework.web.reactive.resource.GzipResourceResolver
org.springframework.web.servlet.resource.GzipResourceResolver
替换为 Java EE 7 的 DefaultManagedTaskScheduler
org.springframework.scheduling.commonj.DelegatingTimerListener
org.springframework.scheduling.commonj.ScheduledTimerListener
org.springframework.scheduling.commonj.TimerManagerAccessor
org.springframework.scheduling.commonj.TimerManagerFactoryBean
org.springframework.scheduling.commonj.TimerManagerTaskScheduler
替换为 Java EE 7 的 DefaultManagedTaskExecutor
org.springframework.scheduling.commonj.DelegatingWork
org.springframework.scheduling.commonj.WorkManagerTaskExecutor
无替代类
org.apache.commons.logging.LogFactoryService
3. Spring Boot 中的废弃类
3.1. Spring Boot 1.4.x
大部分废弃类只是包名变更,类名保持不变:
- ✅
org.springframework.boot.actuate.system.ApplicationPidFileWriter
→org.springframework.boot.system.ApplicationPidFileWriter
- ✅
org.springframework.boot.yaml.ArrayDocumentMatcher
→ 使用精确字符串匹配 - ✅
org.springframework.boot.test.ConfigFileApplicationContextInitializer
→org.springframework.boot.test.context.ConfigFileApplicationContextInitializer
- ✅
org.springframework.boot.context.embedded.DelegatingFilterProxyRegistrationBean
→org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean
- ✅
org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter
→org.springframework.boot.system.EmbeddedServerPortFileWriter
- ✅
org.springframework.boot.test.EnvironmentTestUtils
→org.springframework.boot.test.util.EnvironmentTestUtils
- ✅
org.springframework.boot.context.embedded.ErrorPage
→org.springframework.boot.web.servlet.ErrorPage
- ✅
org.springframework.boot.context.web.ErrorPageFilter
→org.springframework.boot.web.support.ErrorPageFilter
- ✅
org.springframework.boot.context.embedded.FilterRegistrationBean
→org.springframework.boot.web.servlet.FilterRegistrationBean
- ✅
org.springframework.boot.context.embedded.MultipartConfigFactory
→org.springframework.boot.web.servlet.MultipartConfigFactory
- ✅
org.springframework.boot.context.web.OrderedCharacterEncodingFilter
→org.springframework.boot.web.filter.OrderedCharacterEncodingFilter
- ✅
org.springframework.boot.context.web.OrderedHiddenHttpMethodFilter
→org.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter
- ✅
org.springframework.boot.context.web.OrderedHttpPutFormContentFilter
→org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter
- ✅
org.springframework.boot.context.web.OrderedRequestContextFilter
→org.springframework.boot.web.filter.OrderedRequestContextFilter
- ✅
org.springframework.boot.test.OutputCapture
→org.springframework.boot.test.rule.OutputCapture
- ✅
org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer
→org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
- ✅
org.springframework.boot.context.web.ServletContextApplicationContextInitializer
→org.springframework.boot.web.support.ServletContextApplicationContextInitializer
- ✅
org.springframework.boot.context.embedded.ServletListenerRegistrationBean
→org.springframework.boot.web.servlet.ServletListenerRegistrationBean
- ✅
org.springframework.boot.context.embedded.ServletRegistrationBean
→org.springframework.boot.web.servlet.ServletRegistrationBean
- ✅
org.springframework.boot.test.SpringApplicationContextLoader
→ 使用@SpringBootTest
或org.springframework.boot.test.context.SpringBootContextLoader
- ✅
org.springframework.boot.test.SpringBootMockServletContext
→org.springframework.boot.test.mock.web.SpringBootMockServletContext
- ✅
org.springframework.boot.context.web.SpringBootServletInitializer
→org.springframework.boot.web.support.SpringBootServletInitializer
- ✅
org.springframework.boot.test.TestRestTemplate
→org.springframework.boot.test.web.client.TestRestTemplate
📌 Velocity 支持废弃
org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver
org.springframework.boot.autoconfigure.velocity.*
3.2. Spring Boot 1.5.x
- ✅
org.springframework.boot.context.event.ApplicationStartedEvent
→ 使用ApplicationStartingEvent
- ✅
org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector
→ 使用AutoConfigurationImportSelector
- ✅
org.springframework.boot.actuate.cache.GuavaCacheStatisticsProvider
→ Guava 支持在 Spring 5 中移除 - ✅
org.springframework.boot.loader.tools.Layouts.Module
→ 使用自定义LayoutFactory
- ✅
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
→ 使用org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration
- ✅
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
→ 使用org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
- ✅
org.springframework.boot.actuate.autoconfigure.ShellProperties
→ CRaSH 不再维护
📌 CRaSH 相关类废弃
org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration
org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration.AuthenticationManagerAdapterConfiguration
📌 无替代类
org.springframework.boot.autoconfigure.cache.CacheProperties.Hazelcast
org.springframework.boot.autoconfigure.jdbc.metadata.CommonsDbcpDataSourcePoolMetadata
org.springframework.boot.autoconfigure.mustache.MustacheCompilerFactoryBean
3.3. Spring Boot 2.0.x
- ✅
org.springframework.boot.test.util.EnvironmentTestUtils
→ 使用TestPropertyValues
- ✅
org.springframework.boot.actuate.metrics.web.reactive.server.RouterFunctionMetrics
→ 使用自动配置的MetricsWebFilter
📌 无替代类
org.springframework.boot.actuate.autoconfigure.couchbase.CouchbaseHealthIndicatorProperties
3.4. Spring Boot 2.1.x
- ✅
org.springframework.boot.actuate.health.CompositeHealthIndicatorFactory
→ 使用CompositeHealthIndicator(HealthAggregator, HealthIndicatorRegistry)
- ✅
org.springframework.boot.actuate.health.CompositeReactiveHealthIndicatorFactory
→ 使用CompositeReactiveHealthIndicator(HealthAggregator, ReactiveHealthIndicatorRegistry)
4. 总结
本文整理了从 Spring 4.x 和 Spring Boot 1.4 以来的废弃类,并提供了替代方案。建议在项目升级时参考本文,及时替换废弃类,保持代码现代化和兼容性。
📌 小贴士:
- 有默认方法的接口可以直接实现,无需适配器类
- 多数废弃类都有明确替代方案,部分因技术栈过时(如 Velocity、Guava)而废弃
- 遇到废弃类时,优先查官方文档或 Javadoc 确认替代方案