A couple days back I did Spring, AspectJ, Hazelcast Method Caching Aspect and I received an anonymous question on how to do the same thing in Guice I had heard of Guice, I knew what it was, but... I have been a Spring zealot for many years now, so my first reaction was:
What??? Guice? me?? ... Not use Spring? ... why would you ever not do this is Spring?... after a bit more whining I finally found myself saying "Oh ok, let me do it in this Spring wannabe "Juice" framework, just to prove that Spring is better."
All I can say is dammit! I hate being wrong ... the Guice is sweet. It took me about an hour to go from never using it before to having this AOP Method Cache Interceptor project done. A lot of that time was me being a typical developer and playing rather than actually reading the docs. In other words, it's simple, and that is always a good thing.
Now before someone takes this the wrong way: I know Spring does a lot more, Guice is focused on DI, Spring integrates with more, most of the popular Google apps run on Guice so it is rock solid etc. etc... I am not doing this as a comparison and definitely don't want to choose sides, but rather just showing another way of doing something.
I will only be posting the interesting bits of this project here, the full source is available here :
Source Code
First thing, Guice is available in the Maven repo and they recommend using that rather than the google.code site. The POM for this project:
Guice (as they say in their documentation) takes full advantage of annotations and generics. So for this project I created a @Cacheable interface to allow me to annotate the methods I wanted cached.
To configure Guice you need to extend an AbstractModule, this is where you configure what gets injected where by what. In the example below I use a construct called a Provider to allow me to use the Hazelcast factory to inject and instance. One other important thing to note in the code below is the "requestInjection(cache)" when injecting into interceptors you need to specify that in order to that injection before actually using the interceptor.
The AbstractModule:
And lastly for the actual interceptor. Right at the bottom of the source below you will see the @CacheName interface, this is one of a number of ways to map configuration values to be injected. The interceptor implements MethodInterceptor and you get all the required information about the method execution from the MethodInvocation parameter.
So there you go Mr Anonymous, hope it helps.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have...
-
I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regar...
-
I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev . I have to say, I do agree with some o...
-
This series of posts will be about me getting to grips with JBoss Drools . The reasoning behind it is: SAP bought out my company's curre...
-
Update: Check out my updated re-certification on the new 2019 exam... here Let me start by saying, for this certification I studied and...
Thank you. Appreciate your effort in sharing this. I was the anonymous guy ;-)The only reason I asked if it was doable in Guice is that we are looking at using Guice for a project and we already have a home grown version of method caching interceptor but it is ugly code. Guice/Spring allow for doing this in comparatively elegant and maintainable way. One question - is AOP absolutely necessary for this? Can Guice alone handle this?
ReplyDeleteNo problem, it was no effort at all, I always enjoy trying something new and when that something new turns out to be as cool as Guice, I should be thanking you.
ReplyDeleteWell it all depends on what you are trying to achieve, the short answer is ... AOP is never absolutely necessary. The point of AOP is actually to be included or removed without the rest of your application / code noticing. It's just another option and all it does nicely is help with the separation of concerns.
Dude this is the sweetness.
ReplyDeleteThanks for this content on the blog.
ReplyDeleteLiên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ
đặt vé máy bay từ mỹ về việt nam
vé máy bay từ nhật về vn
ve may bay tư duc ve viet nam
vé máy bay từ Toronto về việt nam
ve may bay vietnam airline tu han quoc ve viet nam
chuyen bay chuyen gia trung quoc
From your post, I got many knowledge and increasing my knowledge. Thank you for this. At this time, you have a chance for moving luggage through our Local Moving Services in Atlanta GA on your desire location with safe and sound.
ReplyDelete