Announcement Announcement Module
Collapse
No announcement yet.
How to concatenate results of two util:property-paths? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to concatenate results of two util:property-paths?

    I've got one target bean which I evaluate two property paths on (results are Strings). Than I need to somehow concatenate those Strings and set result as a property on other bean. Something like this:
    Code:
    <bean class="SomeClass">
      <property name="propName">
        <util:property-path path="target.path1"/>:<util:property-path path="target.path2"/>
      <property>
    </bean>
    So if target.path1=VALUE1 and target.path2=VALUE2, than propName should be set to VALUE1:VALUE2.

    But it doesn't work. Is there any way to achieve this?

  • #2
    I was able to get it work, but it looks ugly:

    Code:
    <bean id="mi.parent" bstract="true"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"/>
    
    <bean class="SomeClass">
      <property name="propName">
        <bean parent="mi.parent">
          <property name="targetObject">
            <bean parent="mi.parent">
              <property name="targetObject">
                <bean parent="mi.parent">
                  <property name="targetObject">
                    <bean class="java.lang.StringBuffer">
                      <constructor-arg type="java.lang.String">
                        <util:property-path path="target.path1"/>
                      </constructor-arg>
                    </bean>
                  </property>
                  <property name="targetMethod" value="append"/>
                  <property name="arguments" value=":"/>
                </bean>
              </property>
              <property name="targetMethod" value="append"/>
              <property name="arguments">
                 <util:property-path path="target.path2"/>
              </property>
            </bean>
          </property>
          <property name="targetMethod" value="toString"/>
        </bean>
      <property>
    </bean>
    I do believe it must be easier solution, I just can't find it.

    Comment


    • #3
      I have created simple FactoryBean to do this kind of stuff:

      Code:
      import org.apache.commons.lang.StringUtils;
      import org.springframework.beans.factory.config.AbstractFactoryBean;
      
      /**
       * @author Vasily Ivanov
       */
      public class ValuesJoinFactoryBean extends AbstractFactoryBean
      {
      
        private Object[] values;
        private String separator;
        private int start = Integer.MIN_VALUE;
        private int end = Integer.MAX_VALUE;
      
        protected Object createInstance()
        {
          if (start == Integer.MIN_VALUE || end == Integer.MAX_VALUE) {
            return StringUtils.join(values, separator);
          }
      
          return StringUtils.join(values, separator, start, end);
        }
      
        public Class getObjectType()
        {
          return String.class;
        }
      
        public void setEnd(int end)
        {
          this.end = end;
        }
      
        public void setSeparator(String separator)
        {
          this.separator = separator;
        }
      
        public void setStart(int start)
        {
          this.start = start;
        }
      
        public void setValues(Object[] values)
        {
          this.values = values;
        }
      }
      and configuration:

      Code:
      <bean class="ValuesJoinFactoryBean">
        <property name="values">
          <list>
             <util:property-path path="target.path1"/>
             <value>somestring</value>
             <util:property-path path="target.path2"/>
          </list>
        </property>
        <property name="separator" value=":"/>
      </bean>
      All properties are optional with "smart" defaults.

      Hope that would be helpful to anyone. Feel free to include in your software.

      Comment


      • #4
        Nice. I suggest to post it to Jira as new feature. Maybe it gets integrated into Spring core.

        Comment


        • #5
          Originally posted by Andreas Senft View Post
          Nice. I suggest to post it to Jira as new feature. Maybe it gets integrated into Spring core.
          Thanks. Issue posted:
          http://opensource.atlassian.com/proj...rowse/SPR-3676

          Comment


          • #6
            Has there been any movement on solving this problem more elegantly without having to write my own Factory class, like above? Seems like it should be a VERY common problem and should have a common solution.

            Comment

            Working...
            X