繁体   English   中英

将@value与PropertyPlaceholderConfigurer一起使用

[英]using @value with PropertyPlaceholderConfigurer

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

reportTemplate = reports/report5.jrxml

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>

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/applicationContext-reports.xml
    </param-value>
</context-param>

在我的控制器中我有:

private @Value("${reportTemplate}") String reportTemplatePath;

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

System.out.println("reportTemplatePath="+reportTemplatePath);

而不是输出: 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";
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM