Automatic Proxy creation in Spring
I had some problems with this and found the documentation a little light on the ground, so I felt a little primer was in order.
So what is a proxy class?
Well, in Java terms a proxy class is a runtime-created class which acts like a class of a specified set of interfaces. CGLIB is capable of creating proxy classes without having interfaces – it can mimic a specific class.
Why would you want one?
In Spring (and other languages) proxy classes are used to allow the use of AOP interceptors. The proxy implementation calls the Interceptors before/after invoking the underlying object.
What is an Interceptor?
An Interceptor is a class which gets executed before, after, or around a method. By around we mean both before and after. Oh, and it can also be called when a method throws an exception.
Erm, isn’t that what an ‘Advisor’ is for?
Not exactly – in AOP, an Advisor object states “If this happends, do this”. The “do this” is the Interceptor.
So why would I want to use one?
Well, for logging (wrap and log method invocation). For handling transactions. For handling security. For doing fancy things like invoking validation frameworks or updating progress bars.
So how do I use them?
Lets take a simple example straight from the Spring Reference manual. First create a normal bean:
Tony
51
Now create an Interceptor:
Now create a proxy:
com.mycompany.Person
debugInterceptor
If we then request "personTarget" from the ApplicationContext, we get a normal PersonImpl bean. If we request "person" from the ApplicationContext we get a proxy class that mimics the Person interface. All method calls to this proxy gets intercepted by debugInterceptor
What if I don’t want to intercept all methods?
One way is to use a fancy (deep breath) RegexpMethodPointcutAroundAdvisor. Here we create an Advisor object that invokes an Interceptor when a method name matches the given regexp.
.<strong>get.</strong>
.*absquatulate
You use an ApplicationContext rather than a BeanFactory?
Yeah, otherwise proxys don’t work. I’m not sure why. I should probably find out.
So what is automatic proxy creation?
In our simple example, we defined our bean, then defined a proxy. The downside of this is that we could accidently get the actual bean rather than the proxy, which might not be good. Also, we’ll need one proxy per bean – what if we have loads of beans we want to proxy? Automatic proxy creation is a method in Spring of creating a load of Proxy’s simply.
So how do you do it?
Well, lets look at another example (this one lifted from my command framework). first lets create some commands:
Now lets define an Interceptor:
Now lets create proxies for them:
*Command
debugInterceptor
Now all beans ending in Command will be proxied using debugInterceptor. If you call getBean("someCommand") you’ll get a proxy for the bean rather than the bean itself. We could of listed the bean names, instead we used a regexp. Likewise we could of used multiple Interceptors (or even Advice objects like RegexpMethodPointcutAroundAdvisor implementations).
This entry was posted on Sunday, February 29th, 2004 at 10:48 pm and is filed under Java. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
11 Responses to “Automatic Proxy creation in Spring”
Nicely written!
blush
Great little article! I have been strugging to understand this particular area in Spring and your post basically clear everyting up for me! Thanks again!
Glad I could help :-) I think I’ll be writing some more primers like this in the future….
That would be so well appreciated.
Nice article especialy for AOP newbies like me. Thanks.
I am using spring for my latest project.
I am using iBatis.
For transaction I am using TransactionProxyFactoryBean.
Question:
How can I add throw advice for TransactionProxyFactoryBean?
In documentation you have shown only pre and post interceptors for TransactionProxyFactoryBean.
Throws advice work in the same way as pre/post advice, as such it should be a simple matter to roll an interceptor – more detais can be found in Chapter 5 of the spring manual, Aspect Oriented Programming with Spring
God, I thought I would never be able to understand proxies and interceptor in Spring AOP, you proved me that I was wrong
Thx a lot
I must have read the Spring manual a couple of times and could never tell what the difference was between an Advisor and an Interceptor….this makes it much clearer..
thanks!
Thank you for the clarification. I hope they add this to the Spring documenation soon!
Have your say
Fields in bold are required. Email addresses are never published or distributed.
Some HTML code is allowed:
URIs must be fully qualified (eg: http://www.domainname.com) and all tags must be properly closed.
Line breaks and paragraphs are automatically converted.
Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.