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 默认策略变更

📘 Spring 4.0.x 完整废弃列表


2.2. Spring 4.1.x

  • org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapperBeanPropertyRowMapper
  • org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapperSingleColumnRowMapper

📘 Spring 4.1.x 完整废弃列表


2.3. Spring 4.2.x

  • org.springframework.web.servlet.view.document.AbstractExcelViewAbstractXlsView 及其子类 AbstractXlsxViewAbstractXlsxStreamingView
  • org.springframework.format.number.CurrencyFormatterCurrencyStyleFormatter
  • org.springframework.messaging.simp.user.DefaultUserSessionRegistry → 使用 SimpUserRegistry + ApplicationListener<AbstractSubProtocolEvent>
  • org.springframework.messaging.handler.HandlerMethodSelector → 使用 MethodIntrospector
  • org.springframework.core.JdkVersion → 直接通过反射判断 JDK 版本
  • org.springframework.format.number.NumberFormatterNumberStyleFormatter
  • org.springframework.format.number.PercentFormatterPercentStyleFormatter
  • 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

📘 Spring 4.2.x 完整废弃列表


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

📘 Spring 4.3.x 完整废弃列表


2.5. Spring 5.0.x

  • org.springframework.web.reactive.support.AbstractAnnotationConfigDispatcherHandlerInitializer → 使用 AbstractReactiveWebInitializer
  • org.springframework.web.util.AbstractUriTemplateHandlerDefaultUriBuilderFactory
  • org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer → 使用 WebSocketMessageBrokerConfigurer
  • org.springframework.web.client.AsyncRestTemplate → 使用 WebClient
  • org.springframework.web.context.request.async.CallableProcessingInterceptorAdapterCallableProcessingInterceptor 已有默认方法
  • org.springframework.messaging.support.ChannelInterceptorAdapterChannelInterceptor 已有默认方法
  • org.springframework.util.comparator.CompoundComparator → 使用 JDK 8 的 Comparator.thenComparing
  • org.springframework.web.util.DefaultUriTemplateHandlerDefaultUriBuilderFactory(注意 parsePath 默认值变更)
  • org.springframework.web.context.request.async.DeferredResultProcessingInterceptorAdapterDeferredResultProcessingInterceptor 已有默认方法
  • 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.WebMvcConfigurerAdapterWebMvcConfigurer 已有默认方法
  • 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

📘 Spring 5.0.x 完整废弃列表


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

📘 Spring 5.1.x 完整废弃列表


3. Spring Boot 中的废弃类

3.1. Spring Boot 1.4.x

大部分废弃类只是包名变更,类名保持不变:

  • org.springframework.boot.actuate.system.ApplicationPidFileWriterorg.springframework.boot.system.ApplicationPidFileWriter
  • org.springframework.boot.yaml.ArrayDocumentMatcher → 使用精确字符串匹配
  • org.springframework.boot.test.ConfigFileApplicationContextInitializerorg.springframework.boot.test.context.ConfigFileApplicationContextInitializer
  • org.springframework.boot.context.embedded.DelegatingFilterProxyRegistrationBeanorg.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean
  • org.springframework.boot.actuate.system.EmbeddedServerPortFileWriterorg.springframework.boot.system.EmbeddedServerPortFileWriter
  • org.springframework.boot.test.EnvironmentTestUtilsorg.springframework.boot.test.util.EnvironmentTestUtils
  • org.springframework.boot.context.embedded.ErrorPageorg.springframework.boot.web.servlet.ErrorPage
  • org.springframework.boot.context.web.ErrorPageFilterorg.springframework.boot.web.support.ErrorPageFilter
  • org.springframework.boot.context.embedded.FilterRegistrationBeanorg.springframework.boot.web.servlet.FilterRegistrationBean
  • org.springframework.boot.context.embedded.MultipartConfigFactoryorg.springframework.boot.web.servlet.MultipartConfigFactory
  • org.springframework.boot.context.web.OrderedCharacterEncodingFilterorg.springframework.boot.web.filter.OrderedCharacterEncodingFilter
  • org.springframework.boot.context.web.OrderedHiddenHttpMethodFilterorg.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter
  • org.springframework.boot.context.web.OrderedHttpPutFormContentFilterorg.springframework.boot.web.filter.OrderedHttpPutFormContentFilter
  • org.springframework.boot.context.web.OrderedRequestContextFilterorg.springframework.boot.web.filter.OrderedRequestContextFilter
  • org.springframework.boot.test.OutputCaptureorg.springframework.boot.test.rule.OutputCapture
  • org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializerorg.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
  • org.springframework.boot.context.web.ServletContextApplicationContextInitializerorg.springframework.boot.web.support.ServletContextApplicationContextInitializer
  • org.springframework.boot.context.embedded.ServletListenerRegistrationBeanorg.springframework.boot.web.servlet.ServletListenerRegistrationBean
  • org.springframework.boot.context.embedded.ServletRegistrationBeanorg.springframework.boot.web.servlet.ServletRegistrationBean
  • org.springframework.boot.test.SpringApplicationContextLoader → 使用 @SpringBootTestorg.springframework.boot.test.context.SpringBootContextLoader
  • org.springframework.boot.test.SpringBootMockServletContextorg.springframework.boot.test.mock.web.SpringBootMockServletContext
  • org.springframework.boot.context.web.SpringBootServletInitializerorg.springframework.boot.web.support.SpringBootServletInitializer
  • org.springframework.boot.test.TestRestTemplateorg.springframework.boot.test.web.client.TestRestTemplate

📌 Velocity 支持废弃

  • org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver
  • org.springframework.boot.autoconfigure.velocity.*

📘 Spring Boot 1.4.x 完整废弃列表


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

📘 Spring Boot 1.5.x 完整废弃列表


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

📘 Spring Boot 2.0.x 完整废弃列表


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)

📘 Spring Boot 2.1.x 完整废弃列表


4. 总结

本文整理了从 Spring 4.x 和 Spring Boot 1.4 以来的废弃类,并提供了替代方案。建议在项目升级时参考本文,及时替换废弃类,保持代码现代化和兼容性。

📌 小贴士:

  • 有默认方法的接口可以直接实现,无需适配器类
  • 多数废弃类都有明确替代方案,部分因技术栈过时(如 Velocity、Guava)而废弃
  • 遇到废弃类时,优先查官方文档或 Javadoc 确认替代方案


原始标题:Deprecated Classes in Spring