Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Referencing beans in different config files

  1. #1

    Default Referencing beans in different config files

    Woo first post in this category, go me

    I'm trying to setup a standalone that uses database connections. What I
    wanted to do was seperate my dao's and service beans into their own context file, and put the datasource bean in a different config file that is
    modifyable by the user. An example would be:
    /datasource.xml contains the datasource bean
    /myContext.xml contains my dao's which take as a parameter the datasource
    bean

    then I use a classpathcontext loader like so:
    static {
    String[] contextLocations = new String[]
    {"/dataSource.xml","/resources/myContext.xml"};
    applicationContext = new
    ClassPathXmlApplicationContext(contextLocations);
    }

    However it appears the DTD or something is holding me back from doing this..
    Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException:
    Line 24 in XML document from class path resource [resources/myContext.xml]
    is invalid; nested exception is org.xml.sax.SAXParseException: An element
    with the identifier "dataSource" must appear in the document.
    at
    org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefini
    tions(XmlBeanDefinitionReader.java:133)

    Is the only solution to somehow create a 'dumby' bean with the name
    dataSource within the dao context just to satisfy the dtd? that seams stupid to me.. and if so whats the easiest way to create a dumby bean just to satisfy
    the dtd?
    Thanks,
    David

  2. #2
    Join Date
    Aug 2004
    Location
    New York, NY, USA
    Posts
    33

    Default

    David,

    can you post contents of datasource.xml and myContext.xml?
    Regards,
    Dmitriy.

  3. #3

    Default

    Sure thing. dataSource.xml:
    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>org.gjt.mm.mysql.Driver</value>
            </property>
            <property name="maxActive">
              <value>100</value>
            </property>
            <property name="maxIdle">
              <value>30</value>
            </property>
            <property name="maxWait">
              <value>100</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//localhost&#58;3306/blah?autoReconnect=true</value>
            </property>
            <property name="username">
                <value>protectThe</value>
            </property>
            <property name="password">
                <value>innocent</value>
            </property>
        </bean>
    </beans>
    and myContext.xml:
    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>	
        <!-- DAO's -->
        <bean id="mileageDao" class="com.something.MileageDaoImpl">
            <property name="dataSource"><ref local="dataSource"/></property>
        </bean>
    ....
    
        <!-- Services -->
        <bean id="nadaService" class="com.something.ServiceImpl">
            <property name="mileageDao"><ref local="mileageDao"/></property>
        </bean>
    </beans>
    So you can see the xml parser is freaking out because theres no bean named dataSource within the myContext.xml file... even though it is being loaded into the spring context from the other file...
    -David

  4. #4
    Join Date
    Aug 2004
    Location
    New York, NY, USA
    Posts
    33

    Default

    David,

    in myContext.xml use "ref bean" instead of "ref local" i.e.
    <property name="dataSource"><ref bean="dataSource"/></property>

    With "ref local" you can only reference elements defined in the same file whereas "ref bean" alows referencing "global" elements defined in different files.
    Regards,
    Dmitriy.

  5. #5
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    18

    Default

    Try <ref bean="datasource" instead of <ref local="datasource

    http://www.springframework.org/docs/...rties-detailed

    Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.
    --pete

  6. #6
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    18

    Default

    Doh, beat me to the punch.

  7. #7

    Default

    Aha thanks everyone =)
    -David

  8. #8
    Join Date
    Oct 2007
    Posts
    3

    Default Please Help

    I tried out to use this in my xml file to reference to another xml file at same location but i m getting error
    <bean id="loginDataService" class="jkt.hms.login.dataservice.LoginDataServiceI mpl">
    <property name="sessionFactory"><ref bean="sessionFactory"/></property>

    Error is------>Can't resolve reference to bean 'sessionFactory' while setting property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'sessionFactory' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [loggingBean,tracingBeforeAdvisor,tracingAfterAdvis or,loggingThrowsAdvisor,theTracingBeforeAdvice,the TracingAfterAdvice,theLoggingThrowsAdvice,loginDat aService]; root of BeanFactory hierarchy

  9. #9
    Join Date
    Oct 2007
    Location
    Toronto, ON
    Posts
    90

    Default

    Quote Originally Posted by vikas View Post
    I tried out to use this in my xml file to reference to another xml file at same location but i m getting error
    <bean id="loginDataService" class="jkt.hms.login.dataservice.LoginDataServiceI mpl">
    <property name="sessionFactory"><ref bean="sessionFactory"/></property>

    Error is------>Can't resolve reference to bean 'sessionFactory' while setting property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'sessionFactory' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [loggingBean,tracingBeforeAdvisor,tracingAfterAdvis or,loggingThrowsAdvisor,theTracingBeforeAdvice,the TracingAfterAdvice,theLoggingThrowsAdvice,loginDat aService]; root of BeanFactory hierarchy
    vikas, you should either <import> the other xml file or make sure that the ApplicationContext you are using uses both files. For now, your app configuration consists of a single file.

  10. #10
    Join Date
    Oct 2007
    Posts
    3

    Default help me out

    hi mbogoevici,
    thnks for your help, i tried out using import(and it helped me also) but the problem is i want to reference my another xml file which is in another package and my import tag is in xml file which is in WEB-INF FOLDER.

Similar Threads

  1. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  2. Cannot reference my spring config files from another project
    By lorelia in forum SpringSource Tool Suite
    Replies: 4
    Last Post: Aug 30th, 2005, 01:52 AM
  3. Replies: 2
    Last Post: Aug 4th, 2005, 05:16 PM
  4. Unexpected behaviour with multiple config files
    By rgitzel in forum Container
    Replies: 7
    Last Post: Mar 8th, 2005, 07:11 PM
  5. Config files with imports
    By p_d_austin in forum Container
    Replies: 3
    Last Post: Oct 25th, 2004, 11:33 AM

Posting Permissions

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