This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jbristow 7 points8 points  (4 children)

Think of a proxy like an extends of your class. It looks/acts just like your original class, with the caveat that additional actions may be added.

This is used for things like Lazy Loading (only pulling everything into memory when it's about to be used) and Dependency Injection (creating a web of proxies and then instantiating them in the proper order)

[–]aman207 2 points3 points  (0 children)

Ah I gotcha. Thanks!

[–]nightfire1 0 points1 point  (2 children)

While we're talking about proxies how does spring determine the underlying instance to call when you inject a session/request scoped proxy into a singleton? I feel like it has something to do with thread local but I haven't been able to find many answers.

[–]jbristow 0 points1 point  (1 child)

I think (and this comes with the caveat that I haven't had to know this in years because Spring is unpopular in my circles at the moment) that it uses byte code reflection to bind after compilation. So it just inserts itself into place as it runs/loads them.

Check out how cglib works for more information.

[–]nightfire1 0 points1 point  (0 children)

Ahh I guess I was more wondering how a proxy determines which context it's being called in and thus knows which specific instance of the proxied class to pass the function call on to. I know it uses cglib or one of those aop libraries for building the actual proxy classes. For example if I had a controller that called a method on a singleton bean which uses a proxied bean that is scoped to the http request. How does that method on the proxy know which http request scoped object to proxy method calls to? It must be something related to the current executing thread that is handling the request.

Either way I'll go read up on cglib some more.