0

文件的实际位置在“D:\eclipse\projects\issu\src\main\webapp\WEB-INF\spring\spring.properties”

我试过了:

Resource resource = new ClassPathResource("/src/main/webapp/WEB-INF/spring/spring.properties");

Resource resource = new ClassPathResource("/WEB-INF/spring/spring.properties");

Resource resource = new ClassPathResource("classpath:/WEB-INF/spring/spring.properties");

我还在构建路径中添加了“/src/main/webapp”文件夹。

ClassPathResource 找不到它。有任何想法吗?谢谢!:)

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>ISSU</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我的 spring-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.myapps.issu" />

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <tx:annotation-driven />

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/spring/spring.properties" />
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.databaseurl}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="${hibernate.config}" />
        <property name="packagesToScan" value="com.myapps.issu" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions" value="/WEB-INF/tiles.xml" />
    </bean>

    <bean id="issuDao" class="com.myapps.issu.dao.IssuDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="issuService" class="com.myapps.issu.services.IssuServiceImpl">
        <property name="issuDao" ref="issuDao" />
    </bean>
</beans>
4

3 回答 3

2

你试过这个吗

资源资源 = new ClassPathResource("src/main/webapp/WEB-INF/spring/spring.properties");

于 2013-01-23T05:59:03.370 回答
1

将构建路径添加到 /src/main/webapp (通过项目属性)并使用以下代码:Resource resource = new ClassPathResource("/WEB-INF/spring/spring.properties");

于 2013-01-23T06:04:52.720 回答
0

在您的类路径中添加 src\main\webapp\ 文件夹(使用项目属性)并使用它... Resource resource = new ClassPathResource("WEB-INF/spring/spring.properties");

记住不要在路径的开头使用“/”。

于 2016-12-13T12:20:25.133 回答