|
Integrating Spring 3.1 and JPA 2.0 The purpose of this article is to provide some insights into Spring and JPA integration, how it is implemented within Lexaden products portfolio. Spring is the most popular application development framework for enterprise Java™. Millions of developers use Spring to create high performing, easily testable, reusable code without any lock-in. So does Lexaden! Feel free to get acquainted with pros and cons of Spring @Configuration annotation in a separate topic We have incorporated dynamic proxies in spring configuration - and now JPA query interfaces can be used as ordinary Spring beans. They can be injected (or autowired) into any other beans the same way. Let's have a deeper look at how Spring and JPA could be used in java application: Spring configuration with dynamic proxies Dynamic Proxies Dynamic proxies are used to create dynamic interface implementations at runtime. It can be done by means of class java.lang.reflect.Proxy that's why they are called dynamic proxies. Dynamic proxies can be used for many different purposes, e.g. database connection and transaction management, dynamic mock objects for unit testing, and other AOP-like method intercepting purposes. In our case we use dynamic proxies as a nice way to create and execute JPA queries at runtime. See code snippet of OrganisationQueries interface for more details. Creating Proxies You create dynamic proxies using the Proxy.newProxyInstance() method. The newProxyInstance() methods takes 3 parameters:
After Spring initializes bean organisationQueries it creates proxy implementation for OrganisationQueries interface. All calls to the proxy will be forwarded to the handler implementation of the general InvocationHandler interface. InvocationHandler All method calls to the dynamic proxy are forwarded to this InvocationHandler implementation(see in the next section). Here is how the InvocationHandler interface looks like: public interface InvocationHandler{ Object invoke(Object proxy, Method method, Object[] args) throws Throwable; } The proxy parameter passed to the invoke() method is the dynamic proxy object implementing the interface. The Method object passed into the invoke() method represents the method called on the interface the dynamic proxy implements. From the Method object you can get the method name, parameter types, return type, etc. The Object[] args array contains the parameter values passed to the proxy when the method in the interface implemented was called. As you can see in DomainQueriesContext class we incorporated dynamic proxies in spring configuration - and now JPA query interfaces can be used as ordinary Spring beans. They can be injected (or autowired) into any other beans the same way.
Code snippets from Lexaden Admistration Organisation Queries Interface
@Configuration public class DomainQueriesContext { @Bean public QueryInvocationHandler queryInvocationHandler() { return new QueryInvocationHandler(); } @Bean public OrganisationQueries organisationQueries() { return getProxy(OrganisationQueries.class); } private <T> T getProxy(Class<T> clazz) { return (T) Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{clazz}, queryInvocationHandler()); }}
QueryInvocationHandler Configuration |


