2012-01-31 34 views
3

我有属性文件report.properties(\ WEB-INF \类\属性\ report.properties)与条目:使用@value与PropertyPlaceholderConfigurer

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"; 
    } 
} 
+1

你在什么时候打印它? “@ Value”注入之前,我怀疑println正在发生。尝试添加一个['@ PostConstruct'](http://docs.oracle.com/javase/6/docs/api/javax/annotation/PostConstruct.html)方法并在那里输出私有字段或者可以获得的getter在课程结构化和布线后调用。 – andyb 2012-01-31 16:31:55

+0

@andyb:没有!我在@Value声明后打印它为:@Controller public class myController private @Value(“$ {reportTemplate}”)String reportTemplatePath; //其他字段声明... \t @RequestMapping(值= “report.htm”,方法= RequestMethod.GET) 公共字符串showReport()抛出JRException { ... \t \t \t \t系统。通过out.println( “reportTemplatePath =” + reportTemplatePath); \t \t \t \t ... \t \t \t \t回报 “的报告”; } }' – 2012-01-31 16:41:42

回答

7

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

相关问题