I know cglib proxying works by subclassing target class and overriding target class’ methods.
Can anyone tell how exactly dynamic proxy works?
I know it uses interface for proxying, but how exactly method invokation happens through proxy?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
Proxy.newProxyInstance()you can ask for a proxy implementing required interfaces. You need to pass anInvocationHandlertoo, which is called every time you call any proxy method. Then, in your handler, you know which method is called and its parameters, so you can do what you desire, including using a target object.How does Java handle this? Well, it’s done natively, just as the internals of
reflectionand a lot of basic functionality. So, you can emulate this behavior using plain Java.Extended info here.