|
From: Rob B. <cro...@ya...> - 2007-11-12 16:39:31
|
Hello all,
I've come across a situation a few times where I need a 'lazy hierarchy' of beans. I.E. if a lazy bean isn't initialized, many other beans that reference it shouldn't be loaded either.
An example:
<!-- this is implementation 1 and beans that depend on it -->
<bean id="option1" class="option1" lazy-init="true"/>
<bean id="dependOption1" class="dependOption1">
<property name="blah" ref="option1"/>
</bean>
<!-- this is implementation 2 and beans that depend on it -->
<bean id="option2" class="option2" lazy-init="true"/>
<bean id="dependOption2" class="dependOption2">
<property name="blah" ref="option2"/>
</bean>
<!-- The preferred implementation is determined by property file -->
<bean id="needOptionImpl" class="needOptionImpl">
<property name="optionImpl" ref="${propfile.optionImpl}"/>
</bean>
If propfile.optionImpl=option1, I'd like 'option1' and 'dependOption1' loaded. I wouldn't want 'option2' and 'dependOption2' loaded. Unfortunately, because 'dependOption2' isn't lazy and depends on 'option2' it will be loaded as well even though it is lazy. Having to make 'dependOption2' lazy and setup an artificial dependency to control if it's loaded or not isn't an elegant solution. I'm sure there are better approaches, but my initial thought is something like this:
<bean id="dependOption2" class="dependOption2">
<property name="blah" ref="option2" lazy-ref="X"/>
</bean>
Where X is one of:
A) false. This is a regular reference and causes any referenced lazy-init bean to init. If lazy-ref is omitted, false is the default.
B) true. The reference doesn't cause the referenced lazy-init bean to init. If the referenced bean isn't initialized the property setter is not called. If in a constructor this parameter is null.
C) dependent. The reference doesn't cause the referenced lazy-init bean to init. If the referenced bean isn't initialized, this bean isn't initialized either. If a bean has multiple properties/constructor args that are lazy-ref="dependent" the bean is only initialized if ALL the lazy-ref="dependent" beans it references are initialized.
This will allow much finer control over bean initialization and allow an entire hierarchy of beans to be initialized (or not) if the 'root bean' is referenced.
What do you think? Is this possible?
Rob
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|