spring mvc project中属性文件的位置

时间:2018-03-12 08:10:26

标签: java spring spring-mvc

我正在尝试将属性文件的值注入spring mvc项目的控制器中。我使用的是春季版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.UnsatisfiedDependencyException:   创建名称为&#39; studentController的bean时出错&#39;:不满意   通过字段“countryOptions”表达的依赖性&#39 ;;嵌套异常   是org.springframework.beans.factory.BeanExpressionException:   表达式解析失败;嵌套异常是   org.springframework.expression.spel.SpelEvaluationException:EL1021E:   尝试访问该物业时出现问题   &#39; countryOptions&#39;:&#39;使用名称&#39; countryOptions&#39;创建bean时出错:   调用init方法失败;嵌套异常是   java.io.FileNotFoundException:类路径资源[   countries.properties]无法打开,因为它不存在&#39;

我不太清楚属性文件的位置。它应该在WEB-INF文件夹中吗?如果可能的话,我也想对此作一些解释。

谢谢, 阿什利

1 个答案:

答案 0 :(得分:1)

你是对的,属性文件确实存在于src / main / resources中 你可以按照以下方式访问它们

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

或使用注释

@Value( "${property.needed}" )
private String property;
相关问题