从.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 位置:
我的项目中是否有任何缺失或错误的配置?
最佳答案
尝试使用这个:
<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/