sequence
AOP is the abbreviation of aspect-oriented Programming, the Chinese translation is Aspect Oriented Programming. As one of the features of Spring, it is something to learn.
First, the name aspect programming is very reminiscent of object Oriented programming (OOP). We know that object-oriented programming divides the system from the top down into several classes, such as Controller, Service, Dao, etc., which are common in Web projects. But object-oriented programming is weak for horizontal abstraction from left to right, and system-level functionality like logging, permissions, and so on reappear everywhere. To put it simply, section-oriented programming is a supplement to object-oriented programming, which can gather together some scattered and repeated code in the system and abstract it into a section. Here’s an example:
There is a management system for internal use, each interface in the Controller should record the current user name, the current time and other parameters when calling.
It is obvious that the implementation code for this requirement is repeated in each interface. Even if extracted as a public method of a public class, there is a call to that public method in each interface, leading to coupling between classes.
We wanted to strip away the logging code from the original interfaces and make them focus on their own business logic. Then use precompilation or run-time dynamic proxies to add logging to the interface without modifying the interface code. The idea of dynamically weaving code into specified methods and locations of classes is faceted programming.
concept
Some specific concepts of section-oriented programming.
EXPERT ONE ON ONE J2EE DEVELOPMENT WITHOUT EJB chapter 8
-
Advice: Actions performed at a specific join point. The implementation code in this example is enhancement.
-
Pointcut: An umbrella term for a set of join points that specify when an enhancement should be invoked. In this example, which interfaces and locations are specified are the pointcuts.
-
Join point: A point at which facets can be inserted during application execution. The combination of a specific interface and a specified location in the example is the join point.
-
Aspect: a combination of notification (enhancement) and pointcuts.
AOP is mainly implemented by dynamic proxy in Spring, and there are two specific implementations: JDK dynamic proxy and CGLIB dynamic proxy. JDK dynamic proxy is based on interface proxy, CGLIB dynamic proxy is based on inheritance proxy.
JDK dynamic proxy
As Java’s own dynamic proxy technology, the advantage of JDK dynamic proxy is the use of reflection technology to generate proxy classes, relatively high generation efficiency. The disadvantage is that dynamic proxies can only be based on interfaces, and JDK dynamic proxies cannot be used if the target class does not implement any interfaces.
Take a look at an example using the JDK dynamic proxy.
Public class DynamicProxy implements InvocationHandler {/** * private Object target; public DynamicProxy(Object target) { this.target = target; } public static Objectbind(Object target) {
InvocationHandler invocationHandler = new DynamicProxy(target);
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), invocationHandler);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName() + "Method before execution"); // Invoke Object ret = method.invoke(target, args); System.out.println(method.getName() +"After method execution");
returnret; } public static void main(String[] args) { Dao dao = new OrderDao(); dao.doSomeThing(); Dao daoProxy = (Dao) DynamicProxy.bind(dao); daoProxy.doSomeThing(); }}Copy the code
public class OrderDao implements Dao {
@Override
public void doSomeThing() {
System.out.println("test"); }}Copy the code
public interface Dao {
void doSomeThing();
}
Copy the code
Output:test
doBefore SomeThing method is executedtest
doAfter the method is executed, Process Finished withexit code 0
Copy the code
Afterword.
This time I will just comb through the concepts and write down the AOP concepts I understand. Also referred to a lot of articles, are generally the same details are a little different, write out their own understanding for you to see whether there is wrong. By the way, in case later people ask their own hesitating do not know where to start. I didn’t get to know the specific AOP implementation in Spring, but interested students can delve into it for themselves.
My blog address
The resources
What is section-oriented programming AOP?
Relearning the aspects of Spring