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...
-
I recently finished 97 Things every programmer should know . Well to be completely honest I did skim over a couple of the 97, but all and al...
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.
ReplyDeleteplay casino games online:
ReplyDeletehttps://www.oobbg.com
https://www.oobbg.com/theking
https://www.oobbg.com/sands
https://www.oobbg.com/first
https://www.oobbg.com/yes
https://www.oobbg.com/coin
https://www.oobbg.com/33
https://www.oobbg.com/world
https://www.oobbg.com/merit
https://www.oobbg.com/gatsby
https://www.omgka.com
https://www.omgka.com/theking
https://www.omgka.com/sands
https://www.omgka.com/first
https://www.omgka.com/yes
https://www.omgka.com/super
https://www.omgka.com/gatsby
https://www.omgka.com/33
https://www.omgka.com/world
https://www.omgka.com/merit
https://www.bbdd66.com
https://www.bbdd66.com/theking
https://www.bbdd66.com/sands
https://www.bbdd66.com/first
https://www.bbdd66.com/yes
https://www.bbdd66.com/super
https://www.bbdd66.com/gatsby
https://www.bbdd66.com/33
https://www.bbdd66.com/world
https://www.bbdd66.com/merit
https://www.omgab.com
https://www.omgab.com/theking
https://www.omgab.com/sands
https://www.omgab.com/first
https://www.omgab.com/yes
https://www.omgab.com/super
https://www.omgab.com/gatsby
https://www.omgab.com/33
https://www.omgab.com/world
https://www.omgab.com/merit
https://www.omgqq.com
https://www.omgqq.com/thekingcasino
https://www.omgqq.com/sandscasino
https://www.omgqq.com/firstcasino
https://www.omgqq.com/yescasino
https://www.omgqq.com/supercasino
https://www.omgqq.com/gatsbycasino
https://www.omgqq.com/33casino
https://www.omgqq.com/worldcasino
https://www.omgqq.com/merit
https://www.omcyy.com 우리카지노사이트
ReplyDeletehttps://www.omcyy.com/thekingcasino 더킹카지노
https://www.omcyy.com/sandscasino 샌즈카지노
https://www.omcyy.com/firstcasino 퍼스트카지노
https://www.omcyy.com/yescasino 예스카지노
https://www.omcyy.com/supercasino 슈퍼카지노
https://www.omcyy.com/gatsbycasino 개츠비카지노
https://www.omcyy.com/33casino 33카지노
https://www.omcyy.com/worldcasino 월드카지노
https://www.omcyy.com/merit 메리트카지노
Liê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