2013-01-23 79 views
0

該文件的實際位置在「D:\ eclipse \ projects \ issu \ src \ main \ webapp \ WEB-INF \ spring \ spring.properties」ClassPathResource無法訪問彈簧屬性文件(使用Spring Web MVC)

我想:

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目錄/主/ web應用程序」 文件夾到我的構建路徑。

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> 

回答

2

您是否嘗試過這個

資源資源=新使用ClassPathResource(」的src /主/ web應用/ WEB-INF /彈簧/ spring.properties「);

+0

您可能需要將您的屬性文件放在包和引用上,比如yourpackage/spring.properties – zdesam

1

將構建路徑添加到/ src/main/webapp(通過項目屬性)並使用以下代碼:Resource resource = new ClassPathResource(「/ WEB-INF/spring/spring.properties」);

0

在您的類路徑中添加src \ main \ webapp \文件夾(使用項目屬性)並使用此... Resource resource = new ClassPathResource(「WEB-INF/spring/spring.properties」);

切記不要在路徑的開頭使用「/」。