First, aspect-oriented programming Introduction:
When a method of some class is called, preprocessing or post-processing is done before or after the method executes, and the preprocessing or post-processing operation is encapsulated in another class. , the UserService class turns on the transaction before executing the AddUser () or UpdateUser method, commits the transaction after execution, and in almost all database operations, the method of the transaction operation can be extracted and encapsulated into a class. The proxy class is then processed (the target class method is enhanced), and the proxy class object is returned
Ii. terminology related to AOP
Target: A class that needs to be enhanced.
Joinpoint: Connection point, the method that needs to be enhanced on the target class. (These methods can be enhanced or may not be enhanced).
PointCut: pointcut, enhanced connection point (has been enhanced). Pointcut Terminology Special Connection point
Advice: Enhancements/notifications, enhanced methods.
Weaving: weaving, combining pointcuts and notifications, generates a proxy class process.
Proxy: Agent class.
Aspect: Facets, pointcuts, and notifications combine to form a face
Third, JDK dynamic agent simulation AOP
1. Project structure (interface + target class + Slice class + proxy class + test Class)
2. New Interface UserService
Package Hjp.springAOP.jdkProxy; Public Interface userservice { void addUser (); void updateUser ();}
UserService
3, new target class Userserviceimpl, and implement Interface UserService
PackageHjp.springAOP.jdkProxy; Public classUserserviceimplImplementsUserService {@Override Public voidAddUser () {//TODO auto-generated Method StubSystem.out.println ("JDK Add user"); } @Override Public voidUpdateUser () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("JDK Update user"); }}
Userserviceimpl
4. New Slice class Myaspect
Package Hjp.springAOP.jdkProxy; /** * Slice class for storage enhancement * @author */Publicclass myaspect {publicvoid Beafore () { System.out.println ("before"); } Public void After () { System.out.println (' after ');} }
Myaspect
5, New agent class MyFactory
PackageHjp.springAOP.jdkProxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;/*** Factory generated proxy class, purpose: To combine the target class (pointcut) and the Slice Class (notification). * * @authorJiapeng **/ Public classMyFactory { Public StaticUserService CreateService () {//To create a target class FinalUserService UserService =NewUserserviceimpl (); //creating a Slice class FinalMyaspect Myaspect =NewMyaspect (); //using the JDK dynamic Agent to generate the proxy class//The first parameter is the class loader under the current class, the second parameter is an array of interfaces inherited by the target class, and the third parameter is the anonymous inner class//An anonymous inner class is an inner class that has no name, and because there is no name, the anonymous inner class can only be used once, which is often used to simplify code writing, but there is a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interfaceUserService Proxyservice = (userservice) proxy.newproxyinstance (myfactory.class. getClassLoader (), Userservice.getclass (). Getinterfaces (),NewInvocationhandler () {//The invoke method of the processing class is called when each method of the proxy class executes@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method Stub//pre-execution notificationMyaspect.beafore (); //the corresponding method of executing the target classObject obj =Method.invoke (UserService, args); //notification after executionMyaspect.after (); returnobj; } }); returnProxyservice; }}
proxy class
6. New Test class
Package Hjp.springAOP.jdkProxy; Import org.junit.Test; Public class TestApp { @Test publicvoid demo1 () { = Myfactory.createservice (); Userservice.adduser (); Userservice.updateuser (); }}
Test Class
Iv. cglib Simulation of AOP
Cglib bytecode enhancement tool, which is used by the general framework. As long as there are classes, you can enhance
1, the project structure (Cglib's jar package has been packaged by spring, so added spring's main jar package is OK)
2. New Target class Userserviceimpl, no interface
Package Hjp.springAOP.cglibProxy; Public class Userserviceimpl { publicvoid AddUser () { System.out.println ("cglib add User "); Public void UpdateUser () { System.out.println ("cglib update User");} }
Userserviceimpl
3. New Slice Class
Package Hjp.springAOP.cglibProxy; /** * Slice class for storage enhancement * @author */Publicclass myaspect {publicvoid Beafore () { System.out.println ("before"); } Public void After () { System.out.println (' after ');} }
Slice class
4. New Agent Class
PackageHjp.springAOP.cglibProxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;Importorg.eclipse.jdt.internal.compiler.classfmt.FieldInfoWithAnnotation;ImportOrg.springframework.cglib.proxy.Enhancer;ImportOrg.springframework.cglib.proxy.MethodInterceptor;ImportOrg.springframework.cglib.proxy.MethodProxy;/*** Factory generated proxy class, purpose: To combine the target class (pointcut) and the Slice Class (notification). * * @authorJiapeng **/ Public classMyFactory { Public StaticUserserviceimpl CreateService () {//To create a target class FinalUserserviceimpl Userserviceimpl =NewUserserviceimpl (); //creating a Slice class FinalMyaspect Myaspect =NewMyaspect (); //use Cglib to create the proxy class, which is implemented by dynamically creating the target class subclass when the Cglib is running (the subclass is the proxy class, the Java Dynamic Agent is based on the interface or the parent class)//Creating a Core classEnhancer enhancer =Newenhancer (); //Set Parent classEnhancer.setsuperclass (Userserviceimpl.getclass ()); //the proxy class method invokes the callback function, the equivalent JDK Invocationhandler//interface Callback, sub-interface Methodinterceptor to enhance the methodEnhancer.setcallback (NewMethodinterceptor () {@Override//the first three parameters are the same as the JDK dynamic proxy invoke//The fourth parameter is a method proxy PublicObject Intercept (object proxy, method, object[] args, methodproxy methodproxy)throwsThrowable {//TODO auto-generated Method Stub//pre-notification of slice classMyaspect.beafore (); //Target Method//Object object = Method.invoke (Userserviceimpl, args);Object object = Methodproxy.invokesuper (proxy, args);//executes the proxy class (subclass) of the parent class method (the parent class is the target Class), and the effect is the same as above//notice after slicing classMyaspect.after (); returnobject; } }); //To create a proxy class return(Userserviceimpl) enhancer.create (); }}
proxy class
5. New Test class
Package Hjp.springAOP.cglibProxy; Import org.junit.Test; Public class TestApp { @Test publicvoid demo1 () { = Myfactory.createservice (); Userservice.adduser (); Userservice.updateuser (); }}
Test Class
Spring AOP-oriented slicing programming learning notes