views:

36

answers:

3

I'm trying to use a Spring context namespace to build some existing configuration objects in an application. I have defined a context and pretty much have if working satisfactorily - however, I'd like one bean defined by my namespace to implicitly reference another:

Consider the class named 'Node':

public Class Node {
  private String aField;
  private Node nextNode;
  public Node(String aField, Node nextNode) {
  ...
}

Now in my Spring context I have something like so:

<myns:container>
  <myns:node aField="nodeOne"/>
  <myns:node aField="nodeTwo"/>
</myns:container>

Now I'd like nodeOne.getNextNode() == nodeTwo to be true. So that nodeOne.getNextNode() and nodeTwo refer to the same bean instance. These are pretty much the relevant parts I have in my AbstractBeanDefinitionParser:

public AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
  ...
  BeanDefinitionBuilder containerFactory = BeanDefinitionBuilder.rootBeanDefinition(ContainerFactoryBean.class);
  List<BeanDefinition> containerNodes = Lists.newArrayList();
  String previousNodeBeanName;

  // iterate backwards over the 'node' elements
  for (int i = nodeElements.size() - 1; i >= 0; --i) {
    BeanDefinitionBuilder node = BeanDefinitionBuilder.rootBeanDefinition(Node.class);
    node.setScope(BeanDefinition.SCOPE_SINGLETON);

    String nodeField = nodeElements.getAttribute("aField");
    node.addConstructorArgValue(nodeField);

    if (previousNodeBeanName != null) {
      node.addConstructorArgValue(new RuntimeBeanReference(previousNodeBeanName));
    } else {
      node.addConstructorArgValue(null);
    }

    BeanDefinition nodeDefinition = node.getBeanDefinition();
    previousNodeBeanName = "inner-node-" + nodeField;
    parserContext.getRegistry().registerBeanDefinition(previousNodeBeanName, nodeDefinition);

    containerNodes.add(node);
  }
  containerFactory.addPropertyValue("nodes", containerNodes);
}

When the application context is created my Node instances are created and recognized as singletons. Furthermore, the nextNode property is populated with a Node instance with the previous nodes configuration - however, it isn't the same instance.

If I output a log message in Node's constructor I see two instances created for each node bean definition.

I can think of a few workarounds myself but I'm keen to use the existing model. So can anyone tell me how I can pass these runtime bean references so that I get the correct singleton behaviour for my Node instances?

A: 

Beans are either declared singleton or non-singleton (=prototype). Maybe yours are created non-singleton, so test it with the definition in the bean: <bean ... singleton="true" />

Further information: http://static.springsource.org/spring/docs/1.2.9/reference/beans.html#beans-factory-modes

Markus
I set them to be singletons (see code above) - additionally, if I call isSingleton - it returns true.
teabot
+1  A: 

I'm a little confused by the question. When you say nodeOne.getNode() == nodeTwo do you mean nodeOne.getNextNode()? Does something like the following not work?

<bean id="nodeOne" class="org.foo.Node">
    <constructor-arg index="0" value="nodeOne" />
    <constructor-arg index="1" ref="nodeTwo" />
</bean>
<bean id="nodeTwo" class="org.foo.Node">
    <constructor-arg index="0" value="nodeTwo" />
    <constructor-arg index="1"><null/></constructor-arg>
</bean>

Is it because you don't want to specify the nextNode in Spring and want instead to have the linkage setup in code?

How about something like the following code? This looks at all of the beans of Node type and calls setNextNode() on them. This means you need to expose the setter of course. You also might want to have your Nodes implement BeanNameAware so you don't have to do the aField.

public class NextBeanSetter implements InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;
    public void afterPropertiesSet() throws Exception {
        @SuppressWarnings("unchecked")
        Map<?, Node> beanMap = applicationContext.getBeansOfType(Node.class);
        Node previousNode = null;
        for (Node node : beanMap.values()) {
            node.setNextNode(previousNode);
            previousNode = node;
        }
    }
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
Gray
An interesting approach to the problem. However, I have the feeling that this should be possible from within the BeanDefinitionParser.
teabot
+1  A: 

I guess the problem is that you actually have two instances of each node definition - one as a top level node (via registerBeanDefinition), and another inside nodes property of ContainerFactoryBean. Probably, you should put RuntimeBeanReferences to nodes into ContainerFactoryBean.

axtavt
Thanks - this worked.
teabot
I guess that I was creating one named instance and one inner bean instance. I.e - two bean definitions and each a singleton.
teabot