Announcement Announcement Module
Collapse
No announcement yet.
Dynamic property place holder substitution with Spel Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic property place holder substitution with Spel

    Hi everyone,

    I'm trying to combine Spel with property place holder. What I'm trying to achieve is something like this:

    <bean name="myBean" class="test.MyBean>
    <property name="myBooleanProp" value="${#{T(test.MyEnum).MY_ENUM_INSTANCE.key}}"/>
    </bean>

    So, I would like to pass as property key the value of my enum instance resolved with Spel.

    The example above doesn't work. It throws this exception:
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'boolean' for property 'myBooleanProp'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${myEnumInstanceKey}].
    The property place holder isn't resolved.

    Is there a way to achieve this combination?

    Thanks in advance,
    Andrea Tevoi

  • #2
    Originally posted by andrea.tevoi View Post
    Hi everyone,

    I'm trying to combine Spel with property place holder. What I'm trying to achieve is something like this:

    <bean name="myBean" class="test.MyBean>
    <property name="myBooleanProp" value="${#{T(test.MyEnum).MY_ENUM_INSTANCE.key}}"/>
    </bean>

    So, I would like to pass as property key the value of my enum instance resolved with Spel.

    The example above doesn't work. It throws this exception:
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'boolean' for property 'myBooleanProp'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${myEnumInstanceKey}].
    The property place holder isn't resolved.

    Is there a way to achieve this combination?

    Thanks in advance,
    Andrea Tevoi
    Hello,

    Even am in a similar requirement. Can anyone please help?

    Thanks.

    Comment


    • #3
      I was facing the same problem, and here's my solution:

      First declaring the load of the properties file
      Code:
      <util:properties id="properties" location="classpath:job-queries.properties" />
      And now using, where varargs[0] is my dinamic variable. It loads the following key from the file is: ServiceRenewJob.hibernate.query for example.

      Code:
      <property name="queryString" value="#{properties[varargs[0] + 'RenewJob.hibernate.query']}"/>
      Last edited by traduz; Jan 15th, 2013, 10:11 AM.

      Comment


      • #4
        Originally posted by traduz View Post
        I was facing the same problem, and here's my solution:

        First declaring the load of the properties file
        Code:
        <util:properties id="properties" location="classpath:job-queries.properties" />
        And now using, where varargs[0] is my dinamic variable. The key it loads from the file is: ServiceRenewJob.hibernate.query for example.

        Code:
        <property name="queryString" value="#{properties[varargs[0] + 'RenewJob.hibernate.query']}"/>
        Thanks, but what is varargs[0]? Is this the job parameter? In my case, my key name should have the spring batch job parameter's value.

        Thank you.

        Comment


        • #5
          Not the job parameter.
          I have a main class JobRunner:
          Code:
          package com.company.core.batch.launch;
          public class JobRunner
          {
              public static String[] ARGS;
          
                  public static void main(String[] args) throws Exception
                  {
                      ARGS = args;
                   ...
                  }
          And in my xml file
          Code:
          <util:constant id="varargs" static-field="com.company.core.batch.launch.JobRunner.ARGS"/>
          So in my example when i do
          Code:
          java -jar myjarfile.jar Service
          the key is loaded correctly
          Last edited by traduz; Jan 15th, 2013, 10:15 AM.

          Comment


          • #6
            Ok thanks.

            Comment


            • #7
              You are welcome. I edited my answer so now it's complete.

              Comment


              • #8
                Instead of varargs, I substituted the spring batch's job parameters something like below.

                <property name="fileName" value="#{systemProperties[jobParameters['fileType'] + '.fileName']}" />

                Thanks.

                Comment

                Working...
                X