Announcement Announcement Module
Collapse
No announcement yet.
Referencing beans in different config files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    David,

    can you post contents of datasource.xml and myContext.xml?

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            Doh, beat me to the punch.

            Comment


            • #7
              Aha thanks everyone =)
              -David

              Comment


              • #8
                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

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      You can essentially add them to your WebApplicationContext definition in your web.xml file, using something like:
                      Code:
                      <web-app>
                      
                        <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>
                            /WEB-INF/applicationContext.xml,
                            classpath:/full/path/to/context.xml
                          </param-value>
                        </context-param>
                      
                      ...
                      </web-app>
                      You can find more details here:
                      http://static.springframework.org/sp...path-wildcards

                      Comment


                      • #12
                        Thanks

                        hi mbogoevici ,

                        Thanks for the reply i really helped me a lot.

                        Regards
                        Vikas

                        Comment

                        Working...
                        X