Questions tagged [dynamic-proxy]

A Dynamic Proxy is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface.

A Dynamic Proxy is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface. Thus, a dynamic proxy class can be used to create a type-safe proxy object for a list of interfaces without requiring pre-generation of the proxy class, such as with compile-time tools. Method invocations on an instance of a dynamic proxy class are dispatched to a single method in the instance's invocation handler, and they are encoded with a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments.

Dynamic proxy classes are useful to an application or library that needs to provide type-safe reflective dispatch of invocations on objects that present interface APIs. For example, an application can use a dynamic proxy class to create an object that implements multiple arbitrary event listener interfaces (interfaces that extend java.util.EventListener) to process a variety of events of different types in a uniform fashion, such as by logging all such events to a file.

226 questions
192
votes
4 answers

What is the difference between JDK dynamic proxy and CGLib?

In case of the Proxy Design Pattern, What is the difference between JDK's Dynamic Proxy and third party dynamic code generation API s such as CGLib? What is the difference between using both the approaches and when should one prefer one over…
KDjava
  • 2,355
  • 4
  • 17
  • 22
102
votes
2 answers

Alternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)

According to the documentation: [java.lang.reflect.]Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. The newProxyMethod…
Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
76
votes
5 answers

What are Dynamic Proxy classes and why would I use one?

What is a use case for using a dynamic proxy? How do they relate to bytecode generation and reflection? Any recommended reading?
cwash
  • 4,185
  • 5
  • 43
  • 53
68
votes
5 answers

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

Could someone offer up some advice or point out some blogs/articles that could help with making this decision? The proxies seem very foreign to me and I'm hesitant to use them. I like the ability to control Lazy Loading by using virtual properties…
40
votes
3 answers

Performance cost of Java dynamic proxy

Many modern frameworks (Spring, Hibernate) provide very nice dynamic behaviors with use of Java dynamic proxies, but what's the exact performance cost associated with it? Are there public benchmarks available for Sun JVM?
Gennady Shumakher
  • 5,637
  • 12
  • 40
  • 45
32
votes
1 answer

What is a scoped proxy in Spring?

As we know Spring uses proxies to add functionality (@Transactional and @Scheduled for example). There are two options - using a JDK dynamic proxy (the class has to implement non-empty interfaces), or generating a child class using the CGLIB code…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
29
votes
5 answers

What is the meaning of using proxy ( dynamic proxy) in spring framework?

I don't know the meaning of using proxy in spring. what is efficient?
Linh
  • 1,024
  • 2
  • 16
  • 28
23
votes
3 answers

How use IInterceptor in Castle.DynamicProxy?

I wrote an example like this Simple Calculator class : public class Calculator { public int Add(int a, int b) { return a + b; } } implemented "IInterceptor" that provided by DynamicProxy [Serializable] public abstract class…
17
votes
4 answers

Dynamic proxy for concrete classes

I want to define a method interceptor in a Java program in other words I want to have a behaviour which is executed at each method call. This application isn't executed in an application server and therefore I can't use the EJB around invoke…
eliocs
  • 18,511
  • 7
  • 40
  • 52
16
votes
1 answer

Castle Dynamic Proxy not intercepting method calls when invoked from within the class

I have run into a bit of (what I think is) strange behaviour when using Castle's Dynamic Proxy. With the following code: class Program { static void Main(string[] args) { var c = new InterceptedClass(); var i = new…
14
votes
3 answers

Is there a way to create a DynamicObject that supports an Interface?

Can I define a class which derives from DynamicObject and supports an interface (ICanDoManyThings) without having to implement each method in the interface? I'm trying to make a dynamic proxy object, and want the method invocations on this class to…
gap
  • 2,766
  • 2
  • 28
  • 37
14
votes
3 answers

Usefulness of java dynamic proxies vs regular proxies

I need some advice to which scenarios a dynamic proxy would prove more useful than a regular proxy. I've put alot of effort into learning how to use dynamic proxies effectively. In this question, set aside that frameworks like AspectJ can perform…
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
13
votes
3 answers

Why does JDK dynamic Proxy only work with Interfaces?

The JDK Proxy class only accepts interfaces in the factory method newProxyInstance(). Is there a workaround available, or alternative implementations? The use cases are limited if I have to extract methods to an interface in order to enable them…
stacker
  • 68,052
  • 28
  • 140
  • 210
13
votes
3 answers

Dynamic Proxy Bean with Autowiring capability

In a spring based project I am working on, there's a layer of functionality for calling web service. For each web service operation, a method is created with almost same code but with some different, operation specific, information(e.g. service…
Bilal Mirza
  • 2,576
  • 4
  • 30
  • 57
12
votes
3 answers

How to unwrap the original object from a dynamic proxy

what's the best approach to unwrap a dynamic proxy to retrieve the original object beneath? The dynamic proxy has been created using java.lang.reflect.Proxy.newProxyInstance() Thank you.
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
1
2 3
15 16