Announcement Announcement Module
Collapse
No announcement yet.
Can I use abstract beans with java config? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I use abstract beans with java config?

    Hi-

    Have been experimenting with the Java Config code introduced at http://blog.interface21.com/main/200...on-for-spring/.

    I'd like to do something like this:

    @Configuration
    public class MyConfig
    {
    @Bean
    public BookHolder abstractBookHolder()
    {
    // BookHolder properties will be set by children
    return new BookHolder();
    }
    // the rest is the same as Rod's example, including the book() method
    }

    and in my config
    <beans>
    <bean class="test.javaconfig.MyConfig"/>
    <bean class="org.springframework.beans.factory.java.Conf igurationPostProcessor"/>
    <bean id="bookHolder" parent="abstractBookHolder">
    <property name="book" ref="book"/>
    </bean>
    </beans>

    The test code is:
    public static void main(String[] args)
    {
    GenericApplicationContext context = new GenericApplicationContext();
    XmlBeanDefinitionReader rdr = new XmlBeanDefinitionReader(context);
    rdr.setValidationMode(XmlBeanDefinitionReader.VALI DATION_NONE);
    rdr.loadBeanDefinitions(new InputSource(TestMain.class.getResourceAsStream("/test/javaconfig/appContext.xml")));
    context.refresh();

    BookHolder bh = (BookHolder)context.getBean("bookHolder");
    System.out.println(bh);
    }


    The injection of book works if bookHolder is configured with a class to instantiate instead of a parent, but if I try to use the parent I get:

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: Error registering bean with name 'bookHolder' defined in resource loaded through SAX InputSource: Could not resolve parent bean definition 'abstractBookHolder'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'abstractBookHolder' is defined
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'abstractBookHolder' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:355)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:800)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:829)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:800)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanNamesForType(DefaultList ableBeanFactory.java:159)
    at org.springframework.context.support.AbstractApplic ationContext.getBeanNamesForType(AbstractApplicati onContext.java:687)
    at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:397)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:330)
    at test.javaconfig.TestMain.main(TestMain.java:30)


    Is it possible to use the java config for parent beans? The docs don't mention anything about it.

    Thanks a lot,
    -brunk
Working...
X