ClassPathResource无法访问我的spring属性文件(使用Spring web MVC)

时间:2013-01-23 05:24:26

标签: spring spring-mvc

该文件的实际位置位于“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>

my 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>

3 个答案:

答案 0 :(得分:2)

你试过这个吗

资源资源=新的ClassPathResource(“src / main / webapp / WEB-INF / spring / spring.properties”);

答案 1 :(得分:1)

将构建路径添加到/ src / main / webapp(通过项目属性)并使用以下代码:资源资源=新的ClassPathResource(“/ WEB-INF / spring / spring.properties”);

答案 2 :(得分:0)

在类路径中添加src \ main \ webapp \文件夹(使用项目属性)并使用此... 资源资源=新的ClassPathResource(“WEB-INF / spring / spring.properties”);

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