2012-01-31 23 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中聲明它。

相關問題