PDA

View Full Version : Prototypes bean definition scope



dirtyhenry
Mar 13th, 2006, 09:25 AM
Here is my problem. I'd like to have a local scope for bean that are not singleton in my Spring configuration file :
how can i inject the same prototype bean in two other embed beans' properties in a global bean ?

For instance is it possible to write something like :


<bean id="myBigBean" singleton="false">
...
<bean id="myLocalBean1">
...
</bean>
...
<property name="prop1">
<bean>
<property name="subprop1">
<ref="myLocalBean1" />
</property>
</bean>
</property>
<property name="prop2">
<bean>
<property name="subprop2">
<ref="myLocalBean1" />
</property>
</bean>
</property>
</bean>

Costin Leau
Mar 16th, 2006, 08:52 AM
Can you explain in detail what you are looking for?

dirtyhenry
Mar 16th, 2006, 09:00 AM
In fact my need is make a prototype bean A shared by two other beans B and C.

You can't do that by specifying the bean A by itself because, as I need to set its singleton attribute to false, when B and C's definitions will refer to bean A, it will be created twice and B and C won't share the same bean.

And you can't embed the A bean into B or C definition, because, once again, it will be created twice.

So the ideal solution would be the ability to define local embed beans. With such a solution, a super D class would contain both B and C as embed beans (or referring to their own definition), and set a D-locally defined A bean into them. But i'm not sure this can currently actually be done...

Andreas Senft
Mar 16th, 2006, 09:07 AM
It's not very elegant, but you could do the following:

1. Assign the prototype bean A normally to the property of bean B
2. Use a BeanPostProcessor for bean C to extract the property from B and assign its value to the appropriate property of C

Regards,
Andreas

Costin Leau
Mar 16th, 2006, 09:12 AM
Why not use method injection (chapter 3.3.4) - you can set A to be prototype and set it on B and C even if they are singleton. Moreover, you have a hook where you can do some processing if you want to.

dirtyhenry
Mar 16th, 2006, 09:12 AM
The solution I've actually chosen is that i have a super object D which encapsulates A, B and C and then i post-process it to make a B.set(A) and C.set(A).

But indeed, it's not very elegant neither :-)
The key issue is that wiring in Java instead of wiring in Spring configuration file only is wrong. Maybe local embed beans scope could be a future feature of Spring or maybe beans sharing other beans is not in Spring-spirit.

dirtyhenry
Mar 16th, 2006, 09:22 AM
Why not use method injection (chapter 3.3.4) - you can set A to be prototype and set it on B and C even if they are singleton. Moreover, you have a hook where you can do some processing if you want to.

I don't really see what you mean. Making B and C context aware wouldn't help me, is it ? Each time B would try to get A, a new instance of A would be created.

To describe again a little more my need, I need to produce dynamically into my Java Code, some triplets A-B-C, B and C both containing the same A reference.

So, A, B and C are all prototypes and once a triplet is created and linked (B1 with A1 and C1 with A1, B2 with A2 and C2 with A2), i don't need B and C beans to get new A instances.

What use of method injection have you in mind ? Maybe I'm not Spring-experienced enough to see how it would help me...

Andreas Senft
Mar 16th, 2006, 10:51 AM
Maybe local embed beans scope could be a future feature of Spring or maybe beans sharing other beans is not in Spring-spirit.

I think it is not ubiquitous, but I heard about a similar requirement before. Maybe you could add a request for such a feature (or something equivalent) in Jira.

As an afterthought: If I recall correctly, there should be some scripting support for application contexts. Maybe on that basis a more elegant solution could be achieved.

Regards,
Andreas