3

I am a new to spring. I have a bean class with a constructor having single parameter, a uniqueId.

void Sample(String uniqueId){
      this.uniqueId = uniqueId;
}

the bean doesn't have any default constructor. I require this id for some bussiness logic. this uniqueID needs to be UUID.randomUUID().toString().

How can pass this to the bean from the bean configuration xml.

<bean id="Sample" class="com.scribe.dao.Sample">
     <constructor-arg  value="UUID.randomUUID().toString()"/>               
</bean>

this doesn't work. What are my other options? I have also seen an example like this in another post on stackoverflow.<constructor-arg value="uniqueId"/> but the same didnot work for me.Is there any easyway to do this. any help appreciated.

Community
  • 1
  • 1
arvin_v_s
  • 1,036
  • 1
  • 12
  • 18

1 Answers1

11

You need to make use of Spring Expression Language (SpEL) as illustrated here

The bean definition should look like below

<bean id="Sample" class="com.scribe.dao.Sample">
    <constructor-arg  value="#{ T(java.util.UUID).randomUUID().toString() }"/>               
</bean>
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59