Java spring mvc项目中属性文件的位置

Java spring mvc项目中属性文件的位置,java,spring,spring-mvc,Java,Spring,Spring Mvc,我试图将属性文件的值注入SpringMVC项目中的控制器。我使用的是spring版本5.0.4。 下面是我的servlet.xml的定义 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"

我试图将属性文件的值注入SpringMVC项目中的控制器。我使用的是spring版本5.0.4。 下面是我的servlet.xml的定义

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/util
            http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="mu.mra" />


    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <util:properties id="countryOptions" location="classpath: countries.properties" />

</beans>
属性文件位于src/main/resources文件夹中。但不幸的是,我得到了下面的错误

org.springframework.beans.factory.unsatifiedDependencyException: 创建名为“studentController”的bean时出错:未满足 通过字段“countryOptions”表示的依赖关系;嵌套异常 是org.springframework.beans.factory.BeanExpressionException: 表达式解析失败;嵌套异常是 org.springframework.expression.spel.SpelEvaluationException:EL1021E: 尝试访问该属性时出现问题 “countryOptions”:“创建名为“countryOptions”的bean时出错:” 调用init方法失败;嵌套异常是 java.io.FileNotFoundException:类路径资源[ 无法打开countries.properties],因为它不存在'

我不太确定属性文件的位置。它应该在WEB-INF文件夹中吗?如果可能的话,我也希望对此进行一些解释

谢谢,
Ashley

没错,属性文件确实存在于src/main/resources中 您可以按如下方式访问它们

<util:properties id="countryOptions" location="classpath:countries.properties" />

请检查名称。。。仔细检查一下。。。。特别是classpath:和countries.properties之间的空白。
@Value( "${property.needed}" )
private String property;