Results 1 to 8 of 8

Thread: Dynamic property place holder substitution with Spel

  1. #1

    Default 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. #2
    Join Date
    May 2012
    Posts
    6

    Default

    Quote 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.

  3. #3

    Default

    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 at 09:11 AM.

  4. #4
    Join Date
    May 2012
    Posts
    6

    Default

    Quote 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.

  5. #5

    Default

    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 at 09:15 AM.

  6. #6
    Join Date
    May 2012
    Posts
    6

    Default

    Ok thanks.

  7. #7

    Default

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

  8. #8
    Join Date
    May 2012
    Posts
    6

    Default

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

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

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •