无法在字符串值中解析占位符

时间:2017-03-06 23:42:32

标签: java spring

在我的Spring项目的一个服务中,我有这个:

@Value("${myProject.myVar}")
private int myVar;

这是为了引用名为application.properties的文件中的值,该文件位于我的ProjectRoot/src/main/java文件夹中。该文件看起来像这样:

myProject.myVar = 6

我还有一个配置文件ProjectRoot/WebContent/WEB-INF/spring/servlet-context.xml,其中包含以下行:

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

当我运行项目时,我看到以下内容似乎表明文件正在正确加载:

INFO : org.springframework.context.support.PropertySourcesPlaceholderConfigurer -     
Loading properties file from class path resource [application.properties]

然而,我的程序然后崩溃了这个嵌套异常:

Could not autowire field: private int     
org.myproject.service.MyServiceImpl.myVar; nested 
exception is java.lang.IllegalArgumentException: Could not resolve placeholder 
'myProject.myVar' in string value "${myProject.myVar}"

是什么给出了?

修改:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" 
    version="3.0">
    <display-name>Test</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/spring-security.xml
            /WEB-INF/spring/servlet-context.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/errors/error.jsp</location>
    </error-page>
</web-app>

2 个答案:

答案 0 :(得分:0)

您的application.properties文件位置错误。它应该在 src / main / resources 下,以便加载应用程序上下文。如果您正在运行测试用例,那么它应该在src / test / resources下。

注意,您正在启动Spring启动应用程序

.row

答案 1 :(得分:0)

两个建议

  1. 将您的属性文件放在WEB-INF/classes下,Maven或Gradle等构建工具会自动将src/main/resources下的所有文件放入WEB-INF/classes

  2. 删除=之前和之后的空格。 myProject.myVar = 6成为myProject.myVar=6