Results 1 to 4 of 4

Thread: Combining programmatic BeanFactory with XMl appcontext

  1. #1
    Join Date
    Aug 2005
    Posts
    39

    Default 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. #2
    Join Date
    Dec 2004
    Location
    Rotterdam, The Netherlands
    Posts
    162

    Default

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

    Code:
    Map dataSourceProperties = new HashMap();
    
    dataSourceProperties.put("dataSource.class",
             "org.apache.commons.dbcp.BasicDataSource");
    dataSourceProperties.put("dataSource.driverClassName", databaseProperties
             .getDriverName());
    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(
              applicationContext);
    propReader.registerBeanDefinitions(dataSourceProperties);
    
    // Next load other beans
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
    xmlReader.loadBeanDefinitions(new ClassPathResource("spring-beans.xml"));
    
    applicationContext.refresh();
    Last edited by Kees de Kooter; Feb 3rd, 2006 at 08:59 AM.
    Kees de Kooter
    www.boplicity.net

  3. #3
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    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.
    Bill

  4. #4
    Join Date
    Dec 2004
    Location
    Rotterdam, The Netherlands
    Posts
    162

    Default

    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.
    Kees de Kooter
    www.boplicity.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •