Spring static proxy and dynamic proxy code explanation

Key points of this section:

Java static proxy and JDK dynamic proxy

1 problems encountered in object-oriented design

In traditional OOP programming, objects are the core, and a complete software function is formed through the cooperation between objects. Because objects can inherit, we can abstract the attributes with the same functions or characteristics into a hierarchical class structure system. With the continuous expansion of software specifications, more and more specialized division of labor, and the continuous increase of OOP application practice, some problems that OOP can not solve well are also exposed.

Now suppose that there are three completely similar codes in the system. These codes are usually completed by "copy" and "paste". The software developed in this way is shown in the figure:

Maybe readers have found the shortcomings of this approach. If one day, the code with a blue background needs to be modified, do you want to modify three places at the same time? If not only these three places contain this code, but 100 or even 1000 places, what will be the consequences?

Logging is ubiquitous in code - let's take a look at an example:

In order to track the running process of an application, many methods need to record log information. We usually write this:

Question: what are the disadvantages?

L confused the responsibilities of the business method itself

L huge maintenance workload

2 solution 1

Static agent: 1 You need to know which class the core class (proxy class) is and what methods it has. 2. The non core code needs to be written repeatedly, which makes the structure of the code bloated and forms code redundancy. 3. The non core class (proxy class) needs to implement the core class The interface implemented by (proxy class), that is, they need to implement a common interface, but the interface implemented by the core class (proxy class) shall prevail.

The purpose is to completely separate the business code from the log code and realize loose coupling

L the proxy object and the proxy object must implement the same interface, implement logging related services in the proxy object, and call the proxy object when necessary, while the proxy object only retains the business code

Implementation of static agent

1) Define interface:

2) Proxy class

3) Proxy class

4) Test class

Disadvantages of static proxy:

A proxy interface can only serve one type of object I'm not competent for bigger projects at all

3 solution 2 - dynamic agent

Invocationhandler: each dynamic proxy class must implement the invocationhandler interface, and the instance of each proxy class is associated with a handler. When we call a method through the proxy object, the call of this method will be forwarded to the invoke method of the invocationhandler interface.

At jdk1 After 3, the dynamic agent function that can assist in development is added There is no need to write specific proxy objects for specific objects and methods. Using dynamic proxy, a handler can serve each object A handler's class design must implement Java lang.reflect. Invocationhandler interface The dynamic proxy implemented through the invocationhandler interface can only be the implementation class of the proxy interface

Dynamic agent implementation

1) Handler

2) Factory producing agent object

3) Test class

summary

The above is all about the detailed explanation of spring static proxy and dynamic proxy code in this article. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Description of common spring configuration and parsing classes

Spring MVC interceptor for single sign on

Simple login instance of spring MVC implemented by Java programming

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>