SyntaxHighlighter

Friday, August 24, 2012

@Value injection and Spring Data JPA extensions

@Value injection

a neat little Spring feature is the @Value annotation.  this allows you to directly inject string values and other neat pieces from properties files or other SpEL accessible functions.

a scenario where it is particularly useful is when using customization/extensions to Spring Data JPA.  in particular, i'm going to create a class that requires native sql functions as part of Spring Data JPA.  i want to be able to leverage as much of Spring Data JPA as possible, and the native functions are different from platform to platform (dontcha love sql?).  to assist with this, i can create my implementation class with the particular SQL statement injected using the @Value annotation

so, for the code, here's the entity



here's the custom dao interface



here's the normal Spring Data JPA interface



note: notice the interface extends the custom one.

here's the implementation, including the @Value annotation



and here's how we wire it together



notice the use of the properties placeholder that maps directly to the @Value annotation


No comments:

Post a Comment