tags:

views:

260

answers:

2

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

+1  A: 

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 innovation handler) are only the clue 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.)

Thomas Jung
Why do we implement that? I want to ask about methology or same things
Linh
+1  A: 

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

Bozho