how to use TypeListener, and what's TypeLiteral,TypeEncounter stand for.

664 views
Skip to first unread message

zhan...@gmail.com

unread,
Jun 14, 2013, 6:25:41 AM6/14/13
to google...@googlegroups.com
Hi guys,

I'm new to Guice,  and I want to apply guice in my future project. so I must be a master of it.

there're some spi interface and class can be used to extend.

when I consult com.google.inject.spi.TypeListener, I can't understood when I can use this type. the interface has one method 
<I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter);
the java docs say, 
Listens for Guice to encounter injectable types. If a given type has its constructor injected in one situation but only its methods and fields injected in another, Guice will notify this listener once.
Useful for extra type checking, registering injection listeners, and binding method interceptors.

it's be kindly anyone who can give me an sample about how to use it, and explain how the guice process this type.

thanks,

Eone.

Tim Boudreau

unread,
Jun 16, 2013, 6:57:02 PM6/16/13
to google...@googlegroups.com
It is a thing which looks much more useful than it is :-)

I recall running across it when I was learning Guice and thinking "Ooh, this looks like it will be really handy!" (I was looking for something like Guice 3.1's ProvisionListener).  In practice, every time I have thought "Hey, this must be what TypeListener is good for" it turned out that I was thinking about whatever problem I was having wrong.

If you're new to Guice, you can most likely safely ignore it for a long time, probably forever.  It looks like a path that leads somewhere but my experience has been that there's never something you could do with it that you couldn't do in a more straightforward way without it.  You might use it if you wanted to trigger some work after something is injected, but that's only really useful if it's, say, a library class you don't control - if you wrote the class, there are simpler ways to do those things.

Rooting through my own code, I found one usage in some experimental code - http://j.mp/15aFlbV - to do some automatic MBean registration - and looking at it, I should have just used an eager singleton, so it's just a code-smell there.

Maybe someone else will have a brilliant example where it's actually useful.

-Tim

Eone Zhang

unread,
Jun 16, 2013, 10:00:32 PM6/16/13
to google...@googlegroups.com
Thank you so much for your patience Tim.
I've checked the code you listed, and found that the TypeListener like something in springframework named BeanPostProcessor#postProcessAfterInitialization.
is it right?

-Eone.

在 2013年6月17日星期一UTC+8上午6时57分02秒,Tim Boudreau写道:

Sam Berlin

unread,
Jun 24, 2013, 8:33:05 AM6/24/13
to google...@googlegroups.com
+1 to using ProvisionListener over TypeListener, if all you're interested in is "when is something being created by Guice".  TypeListener is more about telling Guice you have some custom things you want analyzed/injected, so they'll work of Guice's hooks (including injectMembers, MembersInjectors, etc..).

 sam


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

zhangeone

unread,
Jun 26, 2013, 12:08:43 AM6/26/13
to google...@googlegroups.com
it's short but clear reply. thanks sam.

发自我的 iPad
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/VSokCcnCybA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-guice...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages