[英]Using spring3 @Value to access PropertyPlaceholderConfigurer values?
[英]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.