EL Kamel Malek
EL Kamel Malek

Reputation: 101

How can i use a specific proprety for each bean call in Spring

I'm developing an application using spring . I have a bean that i need to call multiple times but for each call i need to change the properties values dynamically . Is there a way to do this .

I had an idea to set the bean properties as an array ,in eatch array i put the parameters that i want to use . For example array[0] contains the params of the first call , array[1] params of the second call,... is it possible to do that ?

Here is a code sample :

<bean class="Dummy2">
    <!-- or a list of values -->
    <property name="foos"> 
        <util:list>
            <value>A,b,c</value>
            <value>X,y,z</value>
            <value>1,2,3</value>
            <value>7,8,9</value>
        </util:list>
    </property>
</bean>

the setter

@Override
public void setFoo(list<String[]> args) {
...
}

If any one have a better idea or a usefull idea i will be grateful

Thank You

This is the propreties of the bean that calls the beans

<property name="activities">
      <list>
        <ref bean="1"/> //Calling bean 1
        <ref bean="2"/> //Calling bean 2
        <ref bean="1"/> //Calling bean 1 again
        <ref bean="2"/>//Calling bean 2 again
        <ref bean="2"/>
      </list>
    </property>

i need to use different parameters for each call (call the setter with different values)

Upvotes: 0

Views: 83

Answers (2)

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

Are you familiar with AOP you can use aspect to do this using @Before Advice to set your method properties before calling it

Upvotes: 1

kevin847
kevin847

Reputation: 1068

Would using Spring's PostConstruct annotation on an initialiser method on your 'master' bean be useful? You could use simple setter methods to inject both the list of beans and the list of configurations into the master bean, and in the init method (annotated with @PostConstruct) configure each bean correctly. http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations

Upvotes: 1

Related Questions