In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Spring AOP is based on the agent, so it is necessary to have a simple understanding of the agent first, which is briefly explained in combination with the code below.
1. First define an interface and entity object public class Student {public String name; public int age;} public interface IStudentService {/ * add student * * / void addStudent (Student student); / * * delete student * * / void removeStudent (String studentId) } public class DefaultStudentService implements IStudentService {@ Override public void addStudent (Student student) {System.out.println ("add student information");} @ Override public void removeStudent (String studentId) {System.out.println ("delete student information with ID" + studentId+ ");}} 2 dynamic proxy technology of Java
The dynamic proxy of Java mainly involves the following two types
1) java.lang.reflect.InvocationHandler interface
The interface implemented by the call handler of the proxy instance; each proxy instance has an associated call handler. When a method is called on a proxy instance, the method call is processed and sent to its calling handler's invoke method. * / public interface InvocationHandler {/ * * @ param proxy dynamically generated proxy object instance @ param method the parameter @ return of the method called by the proxy object @ param args returns the value returned from the method call on the proxy instance, and its type must be compatible with the return type defined in the proxy interface. * / public Object invoke (Object proxy, Method method, Object [] args) throws Throwable;}
2) java.lang.reflect.Proxy object
To put it simply, Java dynamic proxy is to create a proxy object of an instance object through the Proxy object of Java. When the proxy object is called, the invoke method of InvocationHandler will be triggered. We can add some additional code to expand the ability in the invoke method. At the same time, in invoke, we can determine which method is called, and then execute this method on the proxied object through reflection.
3. Use the dynamic proxy technology of Java dynamic proxy instance / * Java [proxy entire interface class] / public static void javaDyncProxy () {/ / the target object to be proxied DefaultStudentService studentService = new DefaultStudentService () / / call handler of the proxy [when we call any method through a dynamic proxy object, the call to this method will be forwarded to the invoke method that implements the InvocationHandler interface class to call] StudentServiceInvocationHandler invocationHandler = new StudentServiceInvocationHandler (studentService) / / create proxy object IStudentService service = (IStudentService) Proxy.newProxyInstance (studentService.getClass (). GetClassLoader (), studentService.getClass (). GetInterfaces (), invocationHandler); service.addStudent (new Student ());} public static class StudentServiceInvocationHandler implements InvocationHandler {private IStudentService target; public StudentServiceInvocationHandler (IStudentService studentService) {this.target = studentService } / * * @ param proxy * Real proxy object for real object InvocationHandler itself, for example: StudentServiceInvocationHandler * / @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("Yes, you are already using Proxy"); return method.invoke (target, args);} public static void main (String [] args) {javaDyncProxy ();}
After executing the main method, the resulting printout is as follows
Yes, you are already using Proxy. Add student information 4. Spring AOP.
Some concepts in Spring AOP have been introduced in the previous article, including Advice and Pointcut. Pointcut is used to filter classes and methods, while Advice is responsible for the actual execution point, and through the combination of the two, there is an Advisor (aspect).
Let's first take a look at the relationships between these objects in SpringAOP.
As can be seen from the picture above
1) the Pointcut interface relies on ClassFilter and MethodMatcher interfaces, through which classes and methods are filtered.
2) the Advice interface [is a Tag Interface] contains a large number of subinterfaces, each of which represents a different method execution point (the internal abstract method is where enhanced code is added).
3) the Adisor interface depends on the Advice interface and represents some execution points of all methods in a class. The most commonly used subinterface type, PointcutAdvisor, adds a dependency on the Pointcut interface, which represents some execution points of some methods in some classes.
Let's take a closer look at the implementations of each interface.
Pointcut implementation class diagram
Advice interface diagram
ThrowsAdvice is a Tag interface, and the implementation class has any one or more of the following method implementations:
Public void afterThrowing (Exception ex)
Public void afterThrowing (RemoteException)
Public void afterThrowing (Method method, Object [] args, Object target, Exception ex)
Public void afterThrowing (Method method, Object [] args, Object target, ServletException ex)
Advisor diagram
The above shows the relationship between several key concepts in SpringAOP through the class diagram. Let's take a look at the class diagram of SpringProxy.
1) ProxyCreatorSupport is associated with AopProxyFactory, and DefaultAopProxyFactory objects are created internally by default
2) AopProxyFactory relies on AopProxy interface, and the implementation class of AopProxy includes JdkDynamicAopProxy based on Java dynamic proxy and CglibAopProxy object based on cglib.
3) when we use AopProxy, we will dynamically choose whether to use Java dynamic proxy or cglib-based proxy according to our settings.
The following example code illustrates the above objects of Spring Aop
Use ProxyFactory/** * to define "orientation" and the corresponding enhancement code. * when in use, if no specific Ponitcut is provided, the enhancement will be woven into all methods of the target class. * * / public class StudentServiceAdvice implements MethodBeforeAdvice, AfterReturningAdvice,ThrowsAdvice {@ Override public void before (Method method, Object [] args, Object target) throws Throwable {System.out.println ("calling method" + method.getName () + "before");} @ Override public void afterReturning (Object returnValue, Method method, Object [] args, Object target) throws Throwable {System.out.println ("calling method" + method.getName () + "after return") The name of the} / * * method must be afterThrowing, the first three parameters are optional [all appear at the same time or none of them], and the last parameter cannot be Throwable or subclass. * multiple afterThrowing methods can be defined, and Spring automatically selects the most matching enhancement method. * * / public void afterThrowing (Method method, Object [] args, Object target,RuntimeException e) {System.out.println ("calling method" + method.getName () + "after throwing exception");}} / * only apply enhancements to all methods on the class * / public static void springProxy () {/ / the target object to be proxied DefaultStudentService studentService = new DefaultStudentService () / * * the proxy factory internally uses AopProxy-type agents to create proxy objects according to the proxy configuration. There are two AopProxy implementation classes: * 1) JdkDynamicAopProxy: Java-based dynamic proxy technology * 2) Cglib2AopProxy: CGLib-based dynamic proxy technology * when using ProxyFactory, if you specify the target interface to proxy through the setInterface () method Then use JdkDynamicAopProxy * if SetOptimize (true) is set for the proxy of the class, Cglib2AopProxy is used. * / ProxyFactory factory = new ProxyFactory (); / / set the interface type factory.setInterfaces (studentService.getClass () .getInterfaces ()) to be proxied; / / set the target object factory.setTarget (studentService) of the proxy / / add enhancements to the proxy target [Advice contains crosscutting code and join point information, so it is a simple aspect] factory.addAdvice (new StudentServiceAdvice ()); / / generate proxy instance IStudentService proxy = (IStudentService) factory.getProxy (); / / call method proxy.addStudent (new Student ("test", 22); proxy.removeStudent ("10001") } output after execution: add student information before calling method addStudent, call method addStudent, call method removeStudent, delete student information with ID 10001, call method removeStudent, and use AspectJProxyFactory org.aspectj aspectjweaver 1.9.4 org.aspectj aspectjrt 1. 9.4 @ Aspectpublic class CommAspect {/ * pre-enhancements * all addStudent methods in all target classes * * / @ Before ("execution (* addStudent (..)") Public void defore (JoinPoint joinPoint) {System.out.println ("> befor addStudent");} public static void springAspectProxy () {/ / the target object to be proxied DefaultStudentService studentService = new DefaultStudentService (); / / AspectJ-based agent factory AspectJProxyFactory factory = new AspectJProxyFactory (); / / set proxy target factory.setTarget (studentService) / / add aspect class factory.addAspect (CommAspect.class); / / generate proxy instance IStudentService proxy = (IStudentService) factory.getProxy (); / / call method proxy.addStudent (new Student ("test", 22)); proxy.removeStudent ("10001");}
Output after execution:
>
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.