Take the 2-minute tour ×

I don't know the meaning of using proxy in spring. what is efficient?

share|improve this question
what do you mean "what is efficient?" –  skaffman Feb 9 '10 at 9:54
add comment

3 Answers

up vote 6 down vote accepted

The dynamic proxy is a feature of the JDK. It can be used to implement an interface using an invocation handler.

A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A proxy interface is such an interface that is implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler.

A dynamic proxy has some overhead. For most use cases the overhead won't be significant, though. The real problem is that the (over)use of dynamic proxies makes an application harder to understand and debug. For example a dynamic proxy will show up with mulitple lines in a stacktrace.

Dynamic proxies are often used to implement decorators. One example of this is AOP in Spring. (I don't want to go into the details of AOP and won't use AOP terminology to keep things simple). Where certain concerns are implemented in one class and used in many places. The dynamic proxies (and invocation handlers) are only the glue code (provided by Spring) to intercept the method calls. (Actually, dynamic proxies are only an implementation detail of this capability. Generating classes on the fly is another possibility to implement it.)

share|improve this answer
Why do we implement that? I want to ask about methology or same things –  Linh Feb 9 '10 at 9:10
add comment

Proxies are used by AOP. In short:

Normally you have.

Caller --> Real object

But when, for example, you want automatic transaction management, spring puts a proxy of your real object

Caller --> Proxy --> Real object

where the proxy starts the transaction.

Here is nice article explaining both the essence of proxies and their efficiency (performance) in spring

share|improve this answer
Bozho, it's a very nice explanation. –  lwpro2 Jan 20 '11 at 3:31
add comment

We can add a functionality to Java class by modifying the source/byte code or by using subclass or proxy which embeds the additional functionality and delegates the calls to underlying object.

share|improve this answer
add comment

Your Answer


