将@value与PropertyPlaceholderConfigurer一起使用

a L*_*ner 3 spring spring-annotations

我有属性文件report.properties(\ WEB-INF\classes\properties\report.properties),带有条目:

reportTemplate = reports/report5.jrxml
Run Code Online (Sandbox Code Playgroud)

applicationContext-reports.xml(\ WEB-INF\config\applicationContext-reports.xml)以及条目:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/report.properties"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/applicationContext-reports.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有:

private @Value("${reportTemplate}") String reportTemplatePath;
Run Code Online (Sandbox Code Playgroud)

但是当我打印这个以检查它的价值时:

System.out.println("reportTemplatePath="+reportTemplatePath);
Run Code Online (Sandbox Code Playgroud)

而不是输出:( reports/report5.jrxml取自属性文件)它给出reportTemplatePath=${reportTemplate}

编辑:在此处复制OP评论以获得清晰度并显示其System.out.println位置.

@Controller
public class myController {
    private @Value("${reportTemplate}") String reportTemplatePath;
    // other field declarations... 

    @RequestMapping(value="report.htm", method=RequestMethod.GET) public String showReport() throws JRException{
        ...
        System.out.println("reportTemplatePath="+reportTemplatePath);
        ...
        return "report";
    }
}
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 7

我想这applicationContext-reports.xml属于根应用程序上下文,而控制器是在上下文中声明的DispatcherServlet.如果是这样,请注意,这PropertyPlaceholderConfigurer是基于每个上下文配置的,因此您还需要声明它...-servlet.xml.