属性文件在春季 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/主/资源文件夹中。但不幸的是,我收到下面的错误

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为"学生控制器"的 Bean 时出错:不满意 通过"国家选项"字段表示的依赖性;嵌套异常 is org.springframework.beans.factory.BeanExpressionException: 表达式解析失败;嵌套异常是 org.springframework.expression.spel.SpelEvaluationException: EL1021E: 尝试访问属性时出现问题 'countryOptions': '创建名为 'countryOptions' 的 bean 时出错: 初始化方法调用失败;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [ 国家/地区属性]无法打开,因为它不存在">

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

谢谢 艾希礼

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

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

或使用注释

@Value( "${property.needed}" )
private String property;

最新更新