Tuesday, February 1, 2011

@Value!; How do I inject you, you little $#%@!!



Spring 3 introduces a new annotation, @Value, for injecting values into properties. This is especially useful if you're using the <component-scan/> of spring's context namespace.

I recently need to access tomcat's "webapp.root" variable in a @Controller stereotype class, which was conveniently managed via Spring.

Our webapp's log files are stored in the root directory, and I needed to access this property in order to read the data in the file. The simplest way to get that value injected into the controller's property would be to inject it using this new value.

After some digging, bashing, and cursing, I finally got it to work...(drumroll)...

@Value("#{systemProperties['webapp.root']}/bookin-synchronizer.log}")

I certainly wish that this was more properly documented in Spring's documentation, as it would have saved me some time...oh well, can't have everything.

No comments:

Post a Comment