Results 1 to 7 of 7

Thread: problems loading applicationcontext

  1. #1
    Join Date
    Mar 2005
    Posts
    7

    Default problems loading applicationcontext

    Hi,
    I have a problem when start my web application.
    It seem Tomcat 5.0.27 not load applicationcontext.

    The file web.xml is very simple:
    Code:
      <servlet>
    		<servlet-name>example</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>2</load-on-startup>
      </servlet>
    
      <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    My servlet-context.xml is:
    Code:
    <!--
      - Application context definition for "example" DispatcherServlet.
      -->
    
    <beans>
    	<bean name="/test" class="web.XmlController"/>
    </beans>
    My applicationContext.xml is:

    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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>/properties/jdbc.properties</value>
    			</list>
    		</property>
    	</bean>
    
      <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>$&#123;jdbc.driverClassName&#125;</value></property>
        <property name="url"><value>$&#123;jdbc.url&#125;</value></property>
        <property name="username"><value>$&#123;jdbc.username&#125;</value></property>
        <property name="password"><value>$&#123;jdbc.password&#125;</value></property>
      </bean>
    
      <bean id="trasferimentiDao" class="dao.impl.jdbc.JdbcTrasferimentiDao">
        <property name="dataSource">
          <ref bean="myDataSource"/>
        </property>
      </bean>
    
      <bean id="listeManager" class="domain.logic.ListeImpl">
        <property name="trasferimentiDao"><ref bean="trasferimentiDao"/></property>
      </bean>
    
    </beans>
    My business class is:
    Code:
    public class ListeImpl implements Liste &#123;
    
      private TrasferimentiDao trasferimentiDao;
    
      public void setTrasferimentiDao&#40;TrasferimentiDao trasferimentiDao&#41; &#123;
        this.trasferimentiDao = trasferimentiDao;
      &#125;
    
      /**
       * Stored Procedure LISTA_TRASFERIMENTI
       * @return
       */
      public Collection ListaTrasferimenti&#40;&#41;  throws DataAccessException &#123;
        List output = new ArrayList&#40;&#41;;
        System.out.println&#40;"Iniezione&#58; " + trasferimentiDao&#41;;
        List trasferimenti = &#40;List&#41; trasferimentiDao.getTrasferimenti&#40;&#41;;
        Iterator i = trasferimenti.iterator&#40;&#41;;
        while &#40;i.hasNext&#40;&#41;&#41; &#123;
        // do something
        ........
        return output;
      &#125;
    &#125;
    Application returns a nullPointerException on variable trasferimentiDao.
    I verified that trasferimentiDao is null.
    Why trasferimentiDao si null?

    If I build a unit test:
    Code:
    public class ListeTest  extends junit.framework.TestCase &#123;
      private ApplicationContext applicationContext;
      private Liste listeManager;
    
      protected void setUp&#40;&#41; &#123;
        applicationContext = new FileSystemXmlApplicationContext&#40;"/WEB-INF/applicationContext.xml"&#41;;
        listeManager = &#40;Liste&#41; applicationContext.getBean&#40;"listeManager"&#41;;
      &#125;
    
      public void ListaTrasferimentiTest&#40;&#41;&#123;
        Collection c = listeManager.ListaTrasferimenti&#40;&#41;;
      &#125;
    &#125;
    In above test trasferimentiDao is loaded and all run is OK.

    Thanks in advance for any suggest.
    Andrea

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

    Default

    Try re-naming servlet-context.xml to example-context.xml.

    Or changing your web.xml:
    Code:
    <servlet>
      <servlet-name>example</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
    </servlet>
    See DispatcherServlet initialization parameters for details.

  3. #3
    Join Date
    Mar 2005
    Posts
    7

    Default

    Thanks for your suggest.
    According to what it is written on LanguageReference document:

    ...The default BeanFactory used by the DispatcherServlet is the XmlBeanFactory and the
    DispatcherServlet will on initialization look for a file named [servlet-name]-servlet.xml in the WEB-INF
    directory of your web application.....


    I have renamed servlet-context.xml to example-servlet.xml.
    "example" is the name of DispatcherServlet.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <!--
      - Application context definition for "example" DispatcherServlet.
      -->
    
    <beans>
      <bean name="/test" class="web.XmlController"/>
    
      <bean id="xmlController" class="web.XmlController" >
        <property name="procedura"><ref bean="listeManager"/></property>
      </bean>
    </beans>
    Code:
    public class XmlController implements Controller, InitializingBean &#123;
    
      private Liste procedura;
    
      public void setProcedura&#40;Liste procedura&#41; &#123;
        this.procedura = procedura;
      &#125;
    
      public ModelAndView handleRequest&#40;
        HttpServletRequest request,
        HttpServletResponse response&#41;
        throws ServletException, IOException &#123;
        String nomeProcedura = request.getParameter&#40;"procedura"&#41;;
        System.out.println&#40;"nomeProcedura&#58; "+nomeProcedura&#41;;
        if &#40;nomeProcedura.equals&#40;"LISTA_TRASFERIMENTI"&#41;&#41; &#123;
          Liste procedura = new ListeImpl&#40;&#41;;
          Document document = procedura.xmlListaTrasferimenti&#40;&#41;;
          String strDocument = document.asXML&#40;&#41;;
          PrintWriter out = response.getWriter&#40;&#41;;
        &#125;
        return new ModelAndView&#40;"/test.jsp"&#41;;
      &#125;
    
    
      public void afterPropertiesSet&#40;&#41; throws Exception &#123;
          if &#40;procedura == null&#41;
            throw new ApplicationContextException&#40;"Must set procedura bean property on " + getClass&#40;&#41;&#41;;
      &#125;
    &#125;
    But not run.
    A piece of stack trace is :
    Code:
    INFO&#58; Destroying singletons in factory &#123;org.springframework.beans.factory.suppor
    t.DefaultListableBeanFactory defining beans &#91;/test,xmlController&#93;; parent&#58; org.s
    pringframework.beans.factory.support.DefaultListableBeanFactory defining beans &#91;
    propertyConfigurer,myDataSource,trasferimentiDao,cartellaInfermieristicaDao,list
    eManager,cartellaInfermieristicaManager&#93;; root of BeanFactory hierarchy&#125;
    28-giu-2005 16.08.06 org.springframework.web.servlet.FrameworkServlet initServle
    tBean
    GRAVE&#58; Context initialization failed
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean wit
    h name '/test' defined in ServletContext resource &#91;/WEB-INF/example-servlet.xml&#93;
    &#58; Initialization of bean failed; nested exception is org.springframework.context
    .ApplicationContextException&#58; Must set procedura bean property on class web.XmlC
    ontroller
    org.springframework.context.ApplicationContextException&#58; Must set procedura bean
     property on class web.XmlController
            at web.XmlController.afterPropertiesSet&#40;XmlController.java&#58;73&#41;
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.invokeInitMethods&#40;AbstractAutowireCapableBeanFactory.java&#58;937&#41;
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;334&#41;
    Indicating that ApplicationContext is not loaded.
    Katentim, have you any idea?
    Thanks a lot.

  4. #4
    Join Date
    Jun 2005
    Posts
    13

    Default

    I think you need to change
    Code:
     <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    to
    Code:
     <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    	<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/applicationContext.xml</param-value>
    		</init-param>    
    <load-on-startup>1</load-on-startup>
      </servlet>
    And make sure that location of applicationContext.xml is correct

  5. #5
    Join Date
    Mar 2005
    Posts
    7

    Default

    Thanks Asya.
    However, if I change example-servlet like below it worked,
    otherwise it doesn't load the context.

    Code:
    <beans>
    
    <!--	<bean name="/test" class="web.XmlController"/>-->
    
      <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
            <prop key="/test">xmlController</prop>
          </props>
        </property>
      </bean>
    
      <bean id="xmlController" class="web.XmlController" >
        <property name="listeManager"><ref bean="listeManager"/></property>
      </bean>
    </beans>
    Using this simple mappings, applicationContext is loaded correctly.
    I don't understand this behaviour.
    Someone has an idea of that?

  6. #6
    Join Date
    Aug 2005
    Location
    Belém - Pará - Brazil
    Posts
    1

    Default

    Insert this tags, in your web.xml:


    <web-app>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    ....
    <servlet>
    <servlet-name>SpringContextServlet</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    ......
    </web-pp>
    Breno Barros
    Software Engineer

  7. #7
    Join Date
    Sep 2005
    Location
    St. Louis, MO USA
    Posts
    25

    Default

    Quote Originally Posted by brenobarros
    Insert this tags, in your web.xml:


    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    ......
    Take note of the location of the applicationContext.xml. It is in WEB-INF/ which is the suggested location. In most application servers this isn't a problem since WEB-INF/ is on the classpath. In Tomcat it is NOT, only WEB-INF/classes and WEB-INF/lib are on the classpath and you will get context loading issues.

    Not sure if this will solve your problem, but I thought I would make you aware in case this is affecting your app.

Similar Threads

  1. Replies: 9
    Last Post: Feb 15th, 2012, 12:19 PM
  2. applicationContext Loading Order
    By todds in forum Web
    Replies: 4
    Last Post: Jul 5th, 2008, 12:19 PM
  3. Loading an Spring ApplicationContext in a J2EEContainer
    By julio_helden in forum Container
    Replies: 6
    Last Post: Oct 14th, 2005, 10:52 AM
  4. ApplicationContext class loading
    By harold_finsbury in forum Container
    Replies: 5
    Last Post: Jul 4th, 2005, 05:15 AM
  5. Manage ApplicationContext loading beans
    By vksoft in forum Container
    Replies: 2
    Last Post: Sep 18th, 2004, 02:46 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
  •