Announcement Announcement Module
Collapse
No announcement yet.
Bean Scope with multiple Bean Factories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean Scope with multiple Bean Factories

    Hi,

    I've got a situation where I want to split my applicationContext.xml. This is in part for testing reasons, and also because my main applicationContext.xml is getting massive.

    Question: Is the scope of bean declarations confined to the declaring bean factory?

    For instance, in the example provided below, which doesn't work for me, the datasource can't be seen by the second bean factory and the Spring initialisation fails.

    So there doesn't seem to be any visibility between factories.

    Thanks, Owen

    This is the datasource declaration:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName"><value>com.informix.jdbc.IfxDriver</value></property>
            <property name="url"><value>...</value></property>
    
            <property name="username"><value>devapp</value></property>
            <property name="password"><value>...</value></property>
            
            <property name="initialSize"><value>2</value></property>
            <property name="maxActive"><value>2</value></property>
            <property name="maxIdle"><value>2</value></property>
        </bean>
    </beans>
    This factory has a dependency on the datasource:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<!-- /////////// Hibernate \\\\\\\\\\\\\\\\\\ -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource"/></property>
            <property name="mappingResources">
                <list>
    ...
                </list>
            </property>
            <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.InformixDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
            </property>
        </bean>
        ...    
    </beans>
    Here's the trace:
    Code:
    junit.framework.AssertionFailedError&#58; Exception in constructor&#58; testGetAlerts &#40;org.springframework.beans.factory.BeanDefinitionStoreException&#58; Line 176 in XML document from file &#91;C&#58;\dev\workspace\Adaptation\Test\src\adaptation\test\spring\hibernate\applicationContext.xml&#93; is invalid; nested exception is org.xml.sax.SAXParseException&#58; An element with the identifier "dataSource" must appear in the document.
    org.xml.sax.SAXParseException&#58; An element with the identifier "dataSource" must appear in the document.
    	at org.apache.xerces.parsers.DOMParser.parse&#40;Unknown Source&#41;
    	at org.apache.xerces.jaxp.DocumentBuilderImpl.parse&#40;Unknown Source&#41;
    	at javax.xml.parsers.DocumentBuilder.parse&#40;Unknown Source&#41;
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions&#40;XmlBeanDefinitionReader.java&#58;127&#41;
    	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions&#40;AbstractXmlApplicationContext.java&#58;124&#41;
    	at org.springframework.context.support.AbstractXmlApplicationContext.refreshBeanFactory&#40;AbstractXmlApplicationContext.java&#58;64&#41;
    	at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;239&#41;
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>&#40;FileSystemXmlApplicationContext.java&#58;83&#41;
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>&#40;FileSystemXmlApplicationContext.java&#58;68&#41;
    	at adaptation.common.SpringUtil.initialize&#40;SpringUtil.java&#58;77&#41;
    	at adaptation.common.AbstractDbSpringTestCase.<init>&#40;AbstractDbSpringTestCase.java&#58;56&#41;
    	at adaptation.cl_case.dao.CaseAlertTest.<init>&#40;CaseAlertTest.java&#58;26&#41;
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0&#40;Native Method&#41;
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance&#40;NativeConstructorAccessorImpl.java&#58;79&#41;
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance&#40;DelegatingConstructorAccessorImpl.java&#58;43&#41;
    	at java.lang.reflect.Constructor.newInstance&#40;Constructor.java&#58;313&#41;
    	at junit.framework.TestSuite.createTest&#40;TestSuite.java&#58;131&#41;
    	at junit.framework.TestSuite.addTestMethod&#40;TestSuite.java&#58;114&#41;
    	at junit.framework.TestSuite.<init>&#40;TestSuite.java&#58;75&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest&#40;RemoteTestRunner.java&#58;331&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;369&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;276&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;167&#41;
    &#41;
    	at junit.framework.Assert.fail&#40;Assert.java&#58;47&#41;
    	at junit.framework.TestSuite$1.runTest&#40;TestSuite.java&#58;263&#41;
    	at junit.framework.TestCase.runBare&#40;TestCase.java&#58;127&#41;
    	at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
    	at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
    	at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
    	at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
    	at junit.framework.TestSuite.runTest&#40;TestSuite.java&#58;208&#41;
    	at junit.framework.TestSuite.run&#40;TestSuite.java&#58;203&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;392&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;276&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;167&#41;

  • #2
    It's true. Two bean factories do not know anything about each others contents. However, to use multiple configuration files you do not need to use multiple bean factories.

    You can read in multiple configuration files with one single bean factory. So you have smaller files and no visibility problems. Have a look at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.

    Regards,
    Andreas

    Comment


    • #3
      Re: Bean Scope with multiple Bean Factories

      Just use
      Code:
      <ref bean="dataSource" />
      instead of
      Code:
      <ref local="dataSource" />
      in your second definition-file.

      Comment


      • #4
        Hi SotA

        <ref bean="... works a treat.

        Thanks, Owen

        Comment

        Working...
        X