Aug 28th, 2006, 07:19 AM
retrieving a bean based on constructor args
is there a way to directly access a bean based on the given constructor args?
<bean id="foo" class="com.foo.bar.Baz">
<constructor-arg index="0" value="alpha"/>
<bean id="foo2" class="com.foo.bar.Baz">
<constructor-arg index="0" value="beta"/>
i'm looking for something like
currently, the only thing that i could find close was
but then i've still got to loop over them and do a get() until i find a match, which is just terrible for performance.
Aug 28th, 2006, 08:26 AM
What you describe is IMHO not supported by Spring. Anyway I fail to see the benefit in this approach. Why don't you just use a conditional lookup to "foo" or "foo2" respectively?
You might argue that you have to ensure the bean has to exist, but also no one would hinder you to specify "gamma" as argument, for which no bean exists as well.
But perhaps there are some more details to your use case I haven't considered?
Aug 28th, 2006, 09:03 AM
flexibility was the key here. looping over a collection of "beans of type" then calling "getCode()" until it matches isn't a very cost effective solution w/r/t time.
The "code" which is indx=0 on the constructor doesn't always match the bean id, so clients were looking to simply "findByCode".
Aug 28th, 2006, 09:17 AM
How about a solution using a naming convention? Just name your beans after the code. E.g. Baz_alpha, Baz_beta.
When looking up a "Baz" with argument "alpha" you can directly retrieve the bean "Baz_alpha".