0

I have set under meta-inf/spring.xml the following

  <?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:context="http://www.springframework.org/schema/context"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema
     /jdbc/spring-jdbc.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema
      /tx/spring-tx-4.1.xsd
                   http://www.springframework.org/schema/util  
       http://www.springframework.org/schema/util/spring-util-4.1.xsd">
      <jpa:repositories base-package="com.mycompany.surveyproject.repository" />   
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.mycompany.surveyproject.model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
</bean>  
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/jpa"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean></beans>

And my web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns
  /javaee/web-app_3_0.xsd"
 version="3.0">
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/web-context.xml
        </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 </web-app>

When I do this in my controller

ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring.xml");

I get error that

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring.xml] cannot be opened because it does not exist
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring.xml]; nested exception is java.io.FileNotFoundException:
trooper
  • 4,444
  • 5
  • 32
  • 32
user3278732
  • 1,694
  • 10
  • 31
  • 67
  • 1
    Don't create a new context in your controller, NEVER do that. Unless you want to have memory issues, database starvation, strange tx errors and other nice to debug errors. Use dependency injection to get your beans don't construct a context. – M. Deinum Dec 18 '14 at 18:22
  • hi there, how would i implement it in a different way then? can u help as am beginner in this and lost in tutorials. – user3278732 Dec 18 '14 at 18:27
  • 1
    By reading the reference guide and following one of the starter guides on [Spring.io](http://spring.io) – M. Deinum Dec 18 '14 at 18:29
  • Don't you need to just define a ContextLoadListener to load the spring beans - http://syntx.io/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/. Dispatcher servlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location - http://stackoverflow.com/questions/2769467/what-is-dispatcher-servlet-in-spring – Andy Dufresne Dec 19 '14 at 04:13

0 Answers0