java - 无法从 spring mvc 中的属性文件读取

标签 java spring spring-mvc

从.properties文件读取属性时,我总是得到空值,我使用的是spring框架4.0.5版本,并使用Spring STS IDE, 我的代码是这样的:

@Component
public class MenuClient {

     @Value("${menu.list}")
     private String url;

     public void showUrl() {
         System.out.println("url : " + url); //shows null
     }

}

我的 Spring 配置是这样的

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/assets/**" location="/resources/assets/" />
<annotation-driven />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.mycompany" />

<context:property-placeholder location="classpath:config.properties" />

</beans:beans>

这是我的 config.properties 位置:

file location

我的项目中是否有任何缺失或错误的配置?

最佳答案

尝试使用这个:

  <context:property-placeholder location="classpath*:config.properties" />

如果没有尝试将 config.properties 文件放入 META-INF 文件夹中并使用此:

   <context:property-placeholder
    location="classpath*:META-INF/config.properties" />

关于java - 无法从 spring mvc 中的属性文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200561/

相关文章:

java - 如何在 Android 上绘制两个地理点之间的路线

java - Spring Boot 应用程序无法解析 org.springframework.boot 包

Spring Boot 1.5.3 NoSuchMethodError : org. springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.getResponseStatus()

java - Spring 安全 - 自定义 ExceptionTranslationFilter

java - 分机 JS 5 : How to represent a java form to an ext js model?

java - 将用户提供的 javascript 代码从网页表单存储到数据库时的安全性

java - Eclipse 无缘无故地在捕获的异常处停止

java - 在我的 android 程序中播放 swf 文件 (flash) 的最佳方式?

java - Spring/Hibernate - 实体被隐式持久化

java - Spring MVC 页面上的访问路径属性值