Announcement Announcement Module
Collapse
No announcement yet.
class path resource cannot be opened because it does not exist Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


      • #4
        That I already did.

        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.

        Comment

        Working...
        X