Spring Boot Configuration Processor provides a @DeprecatedConfigurationProperty
annotation for this purpose. The generated metadata file will include any reason/replacement notes, which causes appropriate deprecation warnings to be logged if an annotated property is used.
See here for a basic example. The following snippet from CassandraProperties.java shows a real-world use case in which spring.data.cassandra.cluster-name
was deprecated in favor of spring.data.cassandra.session-name
. Backwards compatibility is handled by simply calling the getter/setter for the replacement property in the getter/setter for the deprecated property:
public String getSessionName() {
return this.sessionName;
}
public void setSessionName(String sessionName) {
this.sessionName = sessionName;
}
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.data.cassandra.session-name")
public String getClusterName() {
return getSessionName();
}
@Deprecated
public void setClusterName(String clusterName) {
setSessionName(clusterName);
}
To achieve the same behavior for properties that aren't mapped to a @ConfigurationProperties
bean you can manually specify them in META-INF/additional-spring-configuration-metadata.json
and add a runtime dependency on org.springframework.boot:spring-boot-properties-migrator
. See Spring Boot docs for reference.
The following snippet from spring-boot-autoconfigure shows a real-world use case in which server.servlet.path
was deprecated in favor of spring.mvc.servlet.path
. Backwards compatibility is handled by the PropertiesMigrationListener, which "Automatically renames the keys that have a matching replacement and log[s] a report of what was discovered.":
{
"name": "server.servlet.path",
"type": "java.lang.String",
"description": "Path of the main dispatcher servlet.",
"defaultValue": "/",
"deprecation": {
"replacement": "spring.mvc.servlet.path",
"level": "error"
}
},
If you set the deprecated property server.servlet.path=/foo
, the replacement property @Value("${spring.mvc.servlet.path}")
will evaluate to /foo
, and the deprecation notice will be logged on startup.