Announcement Announcement Module
No announcement yet.
Combining programmatic BeanFactory with XMl appcontext Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining programmatic BeanFactory with XMl appcontext

    I have an applicationContext.xml that I want to unit-test. The beans in this context refer to other beans, such as a DataSource, which are defined in other contexts. The idea is that I use different DataSource definitions for different environments (e.g. live and test).

    The problem I have is that for testing, I need to use a programmatic BeanFactory (specifically, StaticListableBeanFactory) to generate my DataSource bean. I cannot find a way to combine my XML application context with my programmatic bean factory. If I use ClasspathXmlApplicationContext, I can only specify a parent ApplicationContext, not a parent BeanFactory. How do I specify my StaticListableBeanFactory as the parent of my ClasspathXmlApplicationContext, or otherwise find a way of allowing my XMl context to load beans from the programmatic bean factory?

    As well as the core API, I've looked at the org.apringframework.test package, but that also seems to assume "config locations" rrather than programmtic bean factories.

  • #2
    Here is a snippet, first setup some stuff in code, then load an xml.

    Map dataSourceProperties = new HashMap();
    dataSourceProperties.put("dataSource.driverClassName", databaseProperties
    dataSourceProperties.put("dataSource.url", databaseProperties.getUrl());
    dataSourceProperties.put("dataSource.username", databaseProperties.getUsername());
    dataSourceProperties.put("dataSource.password", databaseProperties.getPassword());
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    // Load from properties
    PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(
    // Next load other beans
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
    xmlReader.loadBeanDefinitions(new ClassPathResource("spring-beans.xml"));
    Last edited by Kees de Kooter; Feb 3rd, 2006, 08:59 AM.


    • #3
      Would setting the parent of the spring-beans.xml context to the datasource context help?

      But why do you need to define your datasource programatically? Why not just define a second XML file and initialize your context using those two files? Also, Spring has some useful TestCase subclasses which make this easy.


      • #4
        In this case the datasource info came from another database. I posted the snippet as an example, not as an exact solution for you problem.