I've been using Tomcat for many years and (for the first time) I've learned that placeholders ($ {...}
) can be used in web.xml
.
(I knew that it could be used in Tomcat-specific configuration files (server.xml
and context.xml
) ...)
Important:
** Just in case ... Placeholders are not a Servlet API specification, so don't get me wrong! !! ** **
Here, let's put the session timeout time as a placeholder and get the actual value from the system property.
web.xml
<session-config>
<session-timeout>${session.timeout}</session-timeout>
<!-- ... -->
</session-config>
If " -Dsession.timeout = 1
"is specified as the VM argument when starting Tomcat, the session timeout time will be 1 minute.
In the default implementation provided by Tomcat, the default value (the value to be applied when the property is undefined cannot be specified in the placeholder. (For example, in the placeholder function provided by Spring Framework, "$ {property name ". : Default value}
" can be expressed)
Therefore, when using the placeholder function provided by Tomcat, do not define the corresponding property in " $ CATALINA_BASE / conf / catalina.properties
"or" system property ( -D property name = property value
) ". It does not work as expected.
If you specify " -Dorg.apache.tomcat.util.digester.PROPERTY_SOURCE = org.apache.tomcat.util.IntrospectionUtils.PropertySource interface implementation class
"as the VM argument when starting Tomcat, the specified class is the default for Tomcat. It is used in preference to the implementation.
Trial ... Create a class that enables the expression "$ {property name: default value}
" supported by the placeholder function provided by Spring Framework and apply it to Tomcat. I will. (** The implementation is pretty rough so don't copy it w **)
pom.xml
<!--Org for development projects.apache.tomcat.util.IntrospectionUtils.Add an artifact that contains a PropertySource-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.20</version>
<scope>provided</scope>
</dependency>
IntrospectionUtils.PropertySource implementation class
package com.example.tomcat.propertysource;
import org.apache.tomcat.util.IntrospectionUtils;
public class CustomTomcatPropertySource implements IntrospectionUtils.PropertySource {
@Override
public String getProperty(String key) {
String[] keyElements = key.split(":");
if (keyElements.length == 2) {
return System.getProperty(keyElements[0].trim(), keyElements[1].trim());
}
return null; //If null is returned, Tomcat's default implementation will be used
}
}
ʻOrg.apache.tomcat.util.IntrospectionUtils.PropertySource Store the jar file containing the interface implementation class in the "
$ CATALINA_BASE / lib"directory, and set the VM argument when starting Tomcat to"
-Dorg.apache. Specify "tomcat.util.digester.PROPERTY_SOURCE = com.example.tomcat.propertysource.CustomTomcatPropertySource`".
web.xml
<session-config>
<session-timeout>${session.timeout:1}</session-timeout>
<!-- ... -->
</session-config>
This will allow you to specify default values for placeholders on Tomcat.
web.xml
wI've introduced it so far, but ... <session-timeout>
is an Integer type on the XSD of web.xml
, so a schema violation error will occur on the IDE, so it's quite subtle. Right.
(After all w) Basically, it seems good not to handle placeholders (environment-dependent values) on web.xml
.
Even if it is used (temporarily), it is not good to use it in a way that violates the schema, so I think it is better to use it only with the parameter values of the ʻinit-param` system.
Recommended Posts