简体   繁体   English

java.io.FileNotFoundException:类路径资源[src / main / webapp / WEB-INF / spring / appServlet / servlet-context.xml]

[英]java.io.FileNotFoundException: class path resource [src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml]

In my Spring Application I have configured a hibernate connection and when I try to test it by a main class but it giving me an Error. 在我的Spring应用程序中,我已经配置了一个hibernate连接,当我尝试通过主类测试它但它给我一个错误。

My main class, 我的主要课程,

    public static void main(String[] args) {

        Patient sac = new Patient();
        sac.setPatient_Id("M2314");
        sac.setPatient_Age(23);
        sac.setPatient_Name("Sac");
        sac.setPatient_Address("SGTY NY");


        System.out.println("load context");
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");

        PatientService patientService = (PatientService) context.getBean("patientService");
        patientService.persistPatient(sac);

        context.close();
}

And my servlet-context.xml: 我的servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />


    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.tela.pms" />

   <beans:bean id="patientService" class="com.tela.pms.service.impl.PatientServiceImpl"/>
   <beans:bean id="patientdao" class="com.tela.pms.dao.impl.PatientDAOImpl"/>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="root" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"></beans:property>
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.tela.pms.domain.Patient</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory">
    </beans:bean>


</beans:beans>

My web.xml: 我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

My folder structure : 我的文件夹结构:

文件夹结构

Exception : 例外:

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.tela.pms.Test.HibernateTester.main(HibernateTester.java:22)
Caused by: java.io.FileNotFoundException: class path resource [src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more

Use ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"); 使用ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");

Notice file: in front of the path. 通知file:在路径前面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套的异常是java.io.FileNotFoundException:无法打开ServletContext资源[/spring/servlet-context.xml] - nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/spring/servlet-context.xml] java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/final-servlet.xml] - java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/final-servlet.xml] java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] 不存在,无法打开 - java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] cannot be opened because it does not exist 嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] - nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] java.io.FileNotFoundException:无法打开类路径资源[WEB-INF / classes / library.properties],因为它不存在 - java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist spring boot FileNotFoundException:无法打开 ServletContext 资源 [/WEB-INF/main-servlet.xml] - spring boot FileNotFoundException: Could not open ServletContext resource [/WEB-INF/main-servlet.xml] java.io.FileNotFoundException:即使文件存在于 src/main/resources 中,类路径资源 - java.io.FileNotFoundException: class path resource even though file exists in src/main/resources FileNotFoundException: 类路径资源 [WEB-INF/dispatcher-servlet.xml] 无法打开,因为它确实存在时不存在 - FileNotFoundException: class path resource [WEB-INF/dispatcher-servlet.xml] cannot be opened because it does not exist when it does exist webapp / WEB-INF或src / main / resources下的资源文件? - resource files under webapp/WEB-INF or src/main/resources? java.io.FileNotFoundException:类路径资源 - java.io.FileNotFoundException: class path resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM