Results 1 to 4 of 4

Thread: class path resource cannot be opened because it does not exist

  1. #1
    Join Date
    Oct 2012
    Location
    Munich, Germany
    Posts
    5

    Default class path resource cannot be opened because it does not exist

    while trying to develop a simple database-application with spring mvc I get the following error..

    Code:
    java.io.FileNotFoundException: class path resource [WEB-INF/dispatcher-servlet.xml] cannot be opened because it does not exist
    	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
    the dispatcher-servlet.xml looks like this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd
    	http://www.springframework.org/schema/util">
    
    	<context:component-scan base-package="net.roseindia.controllers"/>
    
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
    		<property name="prefix">
    			<value>/WEB-INF/views/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
    	
    	<bean id = "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName">
    			<value>com.mysql.jdbc.Driver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:mysql://localhost:3306/springdbtest</value>
    		</property>
    		<property name="username">
    		 	<value>root</value>
    		</property>
    		<property name="password">
    			<value>MySQL2012pwd</value>
    		</property>
    	</bean>
    	
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    	
    	<bean id="personDAO" class="net.roseindia.controllers.PersonDAOIMpl">
    		<property name="jdbcTemplate">
    			<ref bean="jdbcTemplate" />
    		</property>
    	</bean>
    
    </beans>
    and it is called form here:
    Code:
    public ModelAndView helloWorld() {
    		String message = "Hello World, Spring 3.0!";
    		ApplicationContext ac = new ClassPathXmlApplicationContext("WEB-INF/dispatcher-servlet.xml");
    		PersonDAO personDAO = (PersonDAO) ac.getBean("personDAO");
    		System.out.println("Verbindung zur Datenbank erfolgreich hergestellt");
    		return new ModelAndView("helloworld","message",message);
    	}
    I don't know, what's wrong with this, because the mapping via viewResolver works...
    Does anobody have a solution? I'm new to spring...
    perhaps I've forgotten something..

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    WEB-INF isn't part of the classpath so indeed a classpath resource will not be found... Also your code is so flawed... NEVER NEVER NEVER NEVER create an instance of the applicationcontext to get beans that is what you have dependency injection for...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2012
    Location
    Munich, Germany
    Posts
    5

    Default

    Quote Originally Posted by Marten Deinum View Post
    Also your code is so flawed... NEVER NEVER NEVER NEVER create an instance of the applicationcontext to get beans that is what you have dependency injection for...
    Could you explain what's wrong with my code? I want to learn how to write good applications...

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    That I already did.

    Quote Originally Posted by mdeinum
    NEVER create an instance of the applicationcontext to get beans
    If you want to run out of memory, get transaction problems, slow application ofcourse feel free to go along that route...

    I strongly suggest a read of the Spring Reference guide and specially the part about DI and IoC.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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