|
From: Hagen O. <hag...@hp...> - 2004-03-04 21:42:16
|
Hi all,
after looking at pico and hivemind, I finally took the time to dive in
to spring. I got scared of at first, because I just wanted a container
and there is soooo much else in the framework... but after finding
spring-core.jar I got up to speed.
Anyway this it what I want to do:
large pool of containers
| 1
| *
specific pool, that has a component the the parent components depend on.
I am evaluating this design, because I don't want to replicate the
parent pool each time, but its components are dependent on a use case
specific component.
This is what I came up with:
package testing;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
public class ReversedDependencyBeanFactory extends
DefaultListableBeanFactory {
private Map dependentSingeltonCache = new HashMap();
public ReversedDependencyBeanFactory(BeanFactory aParent) {
super(aParent);
}
public ReversedDependencyBeanFactory(Resource aResource, BeanFactory
aParent) throws BeansException {
this(aParent);
(new XmlBeanDefinitionReader(this)).loadBeanDefinitions(aResource);
}
public ReversedDependencyBeanFactory(Resource aResource) throws
BeansException {
this(aResource, null);
}
public Object getBean(String aName) throws BeansException {
String theBeanName = transformedBeanName(aName);
if (dependentSingeltonCache.containsKey(theBeanName)) {
return dependentSingeltonCache.get(theBeanName);
}
try {
return super.getBean(aName);
} catch (BeansException ex) {
RootBeanDefinition theDefinition =
getMergedBeanDefinition(theBeanName, true);
Object theBean = createBean(theBeanName, theDefinition);
if (theDefinition.isSingleton()) {
dependentSingeltonCache.put(theBeanName, theBean);
}
return theBean;
}
}
public void destroySingletons() {
Set singletonCacheKeys = new
HashSet(dependentSingeltonCache.keySet());
for (Iterator theIterator = singletonCacheKeys.iterator();
theIterator.hasNext();) {
String theBeanName = (String) theIterator.next();
Object theInstance = dependentSingeltonCache.remove(theBeanName);
if (theInstance != null) {
destroyBean(theBeanName, theInstance);
}
}
super.destroySingletons();
}
}
I also looked at a BeanFactoryAware FactoryBean, but it get's passed in
the parent container only...
Anyway, this does pass my test cases (create a parent container, that
has a dependency in the child and play around with it). Since I am very
new to spring (2h in the source), I'd like feedback from the group.
Also, I am looking for a way to ask the child, which components of the
parent (with a certain interface) it can instanciate (because it
satifies its dependencies). I get a little confused about the
Root/ChildBeanDefinition... and this has to perform also ;-)
Any suggestions greatly appreciated
Cheers
)-(agen |