Announcement Announcement Module
Collapse
No announcement yet.
PropertyPlaceholderConfigurer optional system property null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyPlaceholderConfigurer optional system property null

    I want to configure my app using placeholders in my spring.xml, PropertyPlaceholderConfigurer, and system properties. For certain placeholders, if no system property exists I want it to default to the null value. I figured out how to have empty string defaults by setting the properties property of PropertyPlaceholderConfigurer, but I have yet to figure out how to default to null. Any suggestions?

    Thanks in advance.

  • #2
    You should not use null since by default, that's the value returned for a non-existing key inside a map.
    So returning null is the same as not having the key inside a map.
    So you can simply delete the key or if you really, really want to return null, use the Map interface and put values in it (a Properties object is a subclass of Map).

    Comment


    • #3
      PropertyPlaceholderConfigurer system property default null

      Thanks for the reply.

      If I understand you correctly, in the properties set into to my PropertyPlaceholderConfigurer bean, instead of trying to configure my key to return a null result, I should just delete it, which would acheive the same result.

      However, if the key is not in PropertyPlaceholderConfigurer's properties, a BeanDefinitionStoreException ("Could not resolve placeholder...") results. Even if I could manage to add my key to PropertyPlaceholderConfigurer's properties with a null value, PropertyPlaceholderConfigurer would throw the exception anyway:

      Code:
      				String propVal = resolvePlaceholder(placeholder, props, this.systemPropertiesMode);
      				if (propVal != null) {
      					// Recursive invocation, parsing placeholders contained in the
      					// previously resolved placeholder value.
      					propVal = parseStringValue(propVal, props, originalPlaceholderToUse);
      					buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
      					if (logger.isDebugEnabled()) {
      						logger.debug("Resolved placeholder '" + placeholder + "' to value [" + propVal + "]");
      					}
      					startIndex = buf.toString().indexOf(this.placeholderPrefix, startIndex + propVal.length());
      				}
      				else if (this.ignoreUnresolvablePlaceholders) {
      					// Proceed with unprocessed value.
      					startIndex = buf.toString().indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
      				}
      				else {
      					throw new BeanDefinitionStoreException("Could not resolve placeholder '" + placeholder + "'");
      				}
      Perhaps a more detailed description of my problem would help.

      My spring.xml looks like so:
      Code:
          
      ...
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="systemPropertiesModeName">
                  <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
              </property>
              <property name="properties">
                  <props>
                      <prop key="someOtherKey">anotherDefaultValue</prop>
                  </props>
              </property>
          </bean>
          
          <bean id="mybean" class="mypackage.MyClass">
              <property name="myProperty" value="${myKey}"/>
      ...
      I want mybean#myProperty to be set to null if the "myKey" JVM system property is not set. If I add myKey to PropertyPlaceholderConfigurer's properties, an empty string is set in mybean#myProperty, which is not what I want.

      Code:
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="systemPropertiesModeName">
                  <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
              </property>
              <property name="properties">
                  <props>
                      <prop key="someOtherKey">anotherDefaultValue</prop>
                      <prop key="myKey"></prop>
                  </props>
              </property>
          </bean>
      The goal here is to make myKey an OPTIONAL system property that PropertyPlaceholderConfigurer will resolve to null if the system property "myKey" is NOT set.

      Currently I'm accomplishing this in code, but I'd rather do it in my spring.xml.

      Thanks again.

      Comment


      • #4
        Try using the null element as the property value instead
        Code:
        <prop...><null/></prop>
        My bad. Don't think thats going to work
        Last edited by sabarish; Nov 16th, 2006, 04:06 PM.

        Comment


        • #5
          I have exactly the same need. I need to define constructor params using PropertyPlaceholderConfigurer, but some values are legitimately null. There does not seem to be a way of specifying this, other than setting a property as the empty string, which is not the same thing as null.

          There doesn't seem to be an option in PropertyPlaceholderConfigurer for setting missing values to null, and there doesn't seem to be an obvious hook to use to override this class to provide this.

          Any suggestions?

          Thanks

          Tim

          Comment


          • #6
            I guess there are some cases where the properties themselves can be null depending on various environments. Please raise a feature on JIRA related to null properties.

            Comment


            • #7
              > Please raise a feature on JIRA related to null properties.
              Done: http://opensource.atlassian.com/proj...rowse/SPR-2969

              Comment

              Working...
              X