3

I have properties file report.properties (\WEB-INF\classes\properties\report.properties) with entry :

reportTemplate = reports/report5.jrxml

and applicationContext-reports.xml (\WEB-INF\config\applicationContext-reports.xml) with entry:

<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>

In my controller I have:

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

But when i print this to check its value as:

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

Instead of output:reports/report5.jrxml (taken from property file ) it gives reportTemplatePath=${reportTemplate}

Edit: Copied OP comment here for clarity and to show where the System.out.println is located.

@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";
    }
}
andyb
  • 43,435
  • 12
  • 121
  • 150
a Learner
  • 4,944
  • 10
  • 53
  • 89
  • 1
    At what point are you printing it? I suspect the println'ing is happening before `@Value` injection. Try adding a [`@PostConstruct`](http://docs.oracle.com/javase/6/docs/api/javax/annotation/PostConstruct.html) method and outputting the private field there or a getter which you can be called after the class is constructed and wired up. – andyb Jan 31 '12 at 16:31
  • @andyb: Nopes! I m printing it after @Value declaration as : `@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"; } }` – a Learner Jan 31 '12 at 16:41

1 Answers1

7

I guess that applicationContext-reports.xml belongs to the root application context, whereas controller is declared in context of DispatcherServlet. If so, note that PropertyPlaceholderConfigurer is configured at per-context basis, therefore you need to declare it in ...-servlet.xml as well.

axtavt
  • 239,438
  • 41
  • 511
  • 482