Results 1 to 8 of 8

Thread: Multiple Context Config files not working in Tomcat

  1. #1
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    10

    Default Multiple Context Config files not working in Tomcat

    I'm trying to get Spring 1.2 configured with Tomcat 5.0.27 and I cannot seem to get mulitple context files working. I have 3 context files:
    dataAccessContext.xml
    applicationContext.xml
    controller-servlet.xml

    I define these in my web.xml file as:
    Code:
    <context-param>
       <param-name>contextConfigLocation</param-name>	    
       <param-value>/WEB-INF/dataAccessContext.xml
                              /WEB-INF/applicationContext.xml
                              /WEB-INF/controller-servlet.xml
       </param-value>
    </context-param>
    I also have defined the ContextLoaderListener in my web.xml as
    Code:
    	<servlet>
    		<servlet-name>context</servlet-name>
    		<servlet-class>org.springframework.web.context.ContextLoaderListener</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    While trying to access my MVC application I get errors saying that some of the objects should appear in the document

    "nested exception is org.xml.sax.SAXParseException: An element with the identifier "userDAO" must appear in the document."

    I have my userDAO defined in my dataAccessContext.xml file and referenced in my applicationContext.xml file

    I can get everything working if I mash it all into the controller-servlet.xml file but that is a huge mess. Does anyone have clues on what I'm doing wrong?

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Can you post the relevant parts of the context files and the stack trace.

    NB: Your controller context will be loaded automatically with the configuration below:
    Code:
    <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    See the ref. doc. for DispatcherServlet

  3. #3
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    10

    Default

    web.xml snippet
    Code:
    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderListener</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet>
    <servlet-name>controller</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
    </servlet>
    dataAccessContext.xml

    Code:
       
    <bean id="transactionManager"      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="userDAO"     class="com.thoughtequity.refinery.integration.dao.hibernate.UserDAOHibernate">
          <property name="sessionFactory">
             <ref local="sessionFactory"/>
          </property>
    </bean>
    applicationContext.xml
    Code:
    	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    		  abstract="true">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="insert*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    	    
    
       <!-- User Service Business Object as an inner bean wrapped by an outer 
          - transactional proxy. 
    	-->
       <bean id="userService"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
          parent="baseTransactionProxy">
          <property name="target">
             <bean id="userServiceTarget" class="com.thoughtequity.refinery.service.impl.UserServiceImpl">
               <property name="userDAO"><ref local="userDAO"/></property>
             </bean>      
          </property>
       </bean>
    controller-servlet.xml
    Code:
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
       <property name="prefix"><value>/WEB-INF/jsp/spring/</value></property>
       <property name="suffix"><value>.jsp</value></property>
    </bean>
    
    <bean id="defaultHandlerMapping"    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
       
    
    <bean name="/signon.do" class="com.thoughtequity.refinery.web.spring.SignonController">
       <property name="userService"><ref bean="userService"/></property>
    </bean>
    StackTrace (during Tomcat Startup)
    Code:
    08&#58;25&#58;07,145 ERROR DispatcherServlet&#58;227 - Context initialization failed
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name '/signon.do' defined in ServletContext resource  /WEB-INF/controller-servlet.xml&#93;&#58; Can't resolve reference to bean 'userService' while setting property 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDe
    finitionException&#58; No bean named 'userService' is defined&#58; org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans &#91;viewResolver,defaultHandlerMapping,/signon.do,/signonForm.do&#93;; root of BeanFactory hierarchy
    org.springframework.beans.factory.NoSuchBeanDefinitionException&#58; No bean named '
    userService' is defined&#58; org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans viewResolver,defaultHandlerMapping,/signon.do,/signonForm.do&#93;; root of BeanFactory hierarchy
    I have the UserService defined in my applicationContext.xml file and the reference to that in the contorller-servlet.xml file. Same error happens at a lower lever when the UserService Bean tries to access the UserDAO when the UserDAO is defined in the dataAccessContext.xml file. I have all my files in the WEB-INF directory and can't seem to figure out what is going wrong. Everything works file if I stick everything in the controller-servlet.xml file

  4. #4
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    10

    Default

    I also have this defined in my web.xml. I forgot to add it on my last post

    Code:
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/dataAccessContext.xml, /WEB-INF/applicationContext.xml, /WEB-INF/controller-servlet.xml</param-value>
    	</context-param>
    The docs define you should use "...(using a comma as a delimiter) to support multiple contexts" but some of the examples don't use commas, instead they are just whitespace delimited. I've tried numerous variations and still had no luck.

  5. #5

    Default

    In your applicationContext.xml you are referencing the userDAO bean as local which will fail since it's defined in dataAccessContext.xml. Use <ref bean="userDAO" /> instead.

    EDIT: Also noticed in your web.xml your ContextLoaderListener is defined as a servlet. If you are using an old container that doesn't support listeners then you need to use the ContextLoaderServlet. Otherwise use <listener> around your ContextLoaderListener. I.e:

    For Servlet 2.3+ use:

    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListe ner
    </listener-class>
    </listener>

    For Servlet 2.2 use

    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

  6. #6
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    10

    Default

    Thanks! That is a huge help and I'd have never recognized the local vs bean difference in the <ref> tag. Stupid cut/paste error

    Is there any benefit to using local over bean when referencing beans?

  7. #7
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Is there any benefit to using local over bean when referencing beans?
    It also allows the XML parser to validate the bean name earlier - at XML document parse time - to alert you to errors ASAP.

  8. #8
    Join Date
    Jan 2013
    Posts
    2

    Lightbulb I got a similar problem

    Quote Originally Posted by katentim View Post
    It also allows the XML parser to validate the bean name earlier - at XML document parse time - to alert you to errors ASAP.
    Hi all,

    I got a quite strange problem,the same exception "An element
    with the identifier..." when I deployed the web project to resin 3 (in-place), but when I deployed this project to resin 3, it was working fine!, could any people can help me on this? Thx a lot!
    P.S. since the "<ref local='xxx'>" is working fine in resin 3, and there are a lot of <re local> in my project, so I don't want to change them to <ref bean>.

    workFlowContext.xml

    <bean id="workFlowContext" class="com.yan.workflow.WorkFlowContext">
    <property name="workFlowDbSessionFactory" ref="workFlowDbSessionFactory" />
    </bean>

    SpringContext.xml
    <bean id="subscrServiceTarget" class="com.yan.serviceimp.flow.archive.Subscrition ServiceImp">
    <property name="subscrDao">
    <ref local="subscrDao"/>
    </property>
    <property name="workFlowContext">
    <ref local="workFlowContext"/>
    </property>
    </bean>

Similar Threads

  1. Replies: 1
    Last Post: Dec 19th, 2007, 03:21 PM
  2. Replies: 2
    Last Post: Jul 29th, 2005, 11:52 AM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Unexpected behaviour with multiple config files
    By rgitzel in forum Container
    Replies: 7
    Last Post: Mar 8th, 2005, 07:11 PM
  5. Multiple config files
    By ultan in forum SpringSource Tool Suite
    Replies: 4
    Last Post: Jan 18th, 2005, 05:11 PM

Posting Permissions

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