Key points of this section:
Java static proxy
Jdk Dynamic Agent
1 Problems encountered in object-oriented design ideas
In traditional OOP programming, objects are core and a complete software function is formed through collaboration between objects. Since objects can be inherited, we can abstract attributes with the same functions or the same characteristics into a clearly hierarchical class structure system. With the continuous expansion of software specifications, the more professional division of labor is becoming more and more series, and the increasing number of OOP application practices has also exposed some problems that OOP cannot solve well.
Now suppose there are three completely similar pieces of code in the system, which are usually completed by "copying" and "pasting". The software developed in this way is shown in the figure:
Readers may have discovered the shortcomings of this approach. If one day the code with a blue background needs to be modified, should we modify three places at the same time? If not only these three places contain this code, but 100, or even 1,000 places, what will be the consequences?
Logging is everywhere in the code -- first look at an example:
In order to track the operation of an application, many methods require logging information. We usually write this:
//See the article "Log4j Introduction" for log4j import org.apache.log4j.Logger;public class Person {private Logger logger = Logger.getLogger(Person.class);public void sleep(){logger.info("Start execution time:" + new Date());System.out.println("Sleeping");logger.info("Execution end time:" + new Date());}public void eating(){logger.info("Start execution time:" + new Date()); Date()");System.out.println("Incubation");logger.info(""Execution end time:" + new Date()");}}Question: What are the disadvantages?
l Confuses the responsibilities of the business method itself
lThe maintenance workload is huge
2 Solution 1
Static proxy:
1. You need to know which class the core class (proxy class) is and what methods are there.
2. Non-core code needs to be repeated multiple times, which makes the code structure appear to be bloated and creates code redundancy.
3. Non-core classes (proxy classes) need to implement interfaces implemented by core classes (proxy classes), that is, they need to implement common interfaces, but the interfaces implemented by core classes (proxy classes) shall prevail.
The purpose of l is to completely separate the business code from the log code and achieve loose coupling.
lThe proxy object and the proxy object must implement the same interface, implement related services for logging in the proxy object, and call the proxy object when needed, while the proxy object only retains the business code.
Implementation of static proxy
1) Define the interface:
public interface IPerson {public abstract void sleep();public abstract void eating();}2) Proxy class
public class Person implements IPerson {public void sleep(){System.out.println("Sleeping");}public void eating(){System.out.println("Eating");}}3) Agent class
import org.apache.log4j.Logger;public class PersonProxy implements IPerson {private IPerson person;private Logger logger = Logger.getLogger(PersonProxy.class);public PersonProxy(IPerson person) {this.person = person;}public void eating() {logger.info("Start execution time:" + new Date()");person.eating();logger.info(""Execution end time:" + new Date()");}public void sleep() {logger.info("start execution time:" + new Date()");person.sleep();logger.info(""execution end time:" + new Date()");}}4) Test Class
package com.aptech.aop2;public class PersonTest {public static void main(String[] args) {IPerson proxy = new PersonProxy(new Person());proxy.eating();proxy.sleep();}}Disadvantages of static proxy:
A proxy interface can only serve one type of object. It is simply incompetent for slightly larger projects.
3 Solution 2-Dynamic Agent
InvocationHandler: Each dynamic proxy class must implement the InvocationHandler interface, and each instance of the 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 for call.
After JDK1.3, a dynamic proxy function that can assist in development was added. There is no need to write specific proxy objects for specific objects and methods. Using dynamic proxying can make a handler serve each object.
A processor's class design must implement the java.lang.reflect.InvocationHandler interface.
Dynamic proxy implemented through the InvocationHandler interface can only proxy the implementation class of the interface.
Dynamic proxy implementation
1) Handler
public class DynaProxyHandler implements InvocationHandler {private Logger logger = Logger.getLogger(DynaProxyHandler.class);private Object target;//Proxy object public void setTarget(Object target) {this.target = target;}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {logger.info("Execution start time:" + new Date());Object result = method.invoke(target, args);logger.info("Execution end time:" + new Date());return result;//Return method execution result}}2) Factory for production agent
import java.lang.reflect.Proxy;public class DynaProxyFactory {//obj is the proxy object public static Object getProxy(Object obj){DynaProxyHandler handler = new DynaProxyHandler();handler.setTarget(obj);return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);}}3) Test Class
public class PersonTest {public static void main(String[] args) {IPerson person = (IPerson) DynaProxyFactory.getProxy(new Person());//Return the proxy class, the proxy class is dynamically created by the JVM in memory. This class implements all interfaces (all methods) of the incoming interface array.person.eating();person.sleep();}}Summarize
The above is all the detailed explanation of Spring static proxy and dynamic proxy code in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Common configuration and analysis class description of Spring
SpringMVC interceptor implements single sign-on
Java programming implementation of springMVC simple login example
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!