In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the spring AOP framework, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Spring AOP (including both annotation-based and profile-based approaches)
Spring AOP? Aspect-oriented programming, different from object-oriented programming OOP
AspectJ: it's the most complete and popular AOP framework in the Java community. Here's an example using aspectJ.
one。 Based on annotation mode
The steps are as follows:
Introduction of jar packages (necessary jar packages for spring and jar packages for aspectj)
Business method HelloworldService (annotated @ Component on the class, put in the spring ioc container)
Aspect LogingAop (add @ Component to the class to add it to the ioc container, and you also need to note @ Aspect to make it an aspect)
Spring configuration file applicationContext.xml (scan package, and configuration of aop generation agent class)
test
The structure is as follows:
HelloworldService code implementation:
Package com.aop.demo;import org.springframework.stereotype.Component;@Componentpublic class HelloworldService {public int add (int I, int j) {return iTunj;}}
LogingAop code implementation:
Package com.aop.demo;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Component@Aspectpublic class LogingAop {@ Pointcut ("execution (public int com.aop.demo.HelloworldService.add (int,int))") public void pointcut () {} @ Before (value= "pointcut ()") public void beforeMethod (JoinPoint jointPoint) {String methodName = jointPoint.getSignature () .getName (); List args = Arrays.asList (jointPoint.getArgs ()); System.out.println ("Method (" + methodName+ ") args (" + args+ ") Start");}}
Configuration in applicationContext.xml
The test code is as follows
Package com.aop.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml"); HelloworldService helloworldService = (HelloworldService) ctx.getBean ("helloworldService"); int result = helloworldService.add (1,2); System.out.println ("Result is" + result);}}
The test results are as follows:
Method (add) args ([1,2]) StartResult is 3
Introduction to concepts in AOP:
Aspect: functions that span multiple modules of an application, such as logging, such as parameter validation, such as modularized special objects
Notification: the method in the section above
Pointcut: equivalent to query conditions, spring automatically generates new proxy objects in which business methods need to be notified
AspectJ supports 5 types of notification comments:
@ Before: pre-notification, executed before the method is executed
@ After: post notification, which is executed after the method is executed (whether there is an exception or not)
@ AfterRuning: returns the notification, which is executed after the method returns the result (only if there are no exceptions)
@ AfterThrowing: exception notification, after the method throws an exception
@ Around: surround the notification and execute around the method
Code example annotated in AspectJ 5
Package com.aop.demo;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component @ Component@Aspectpublic class LogingAop {@ Pointcut ("execution (public int com.aop.demo.HelloworldService.add (int,int)") public void pointcut () {}; @ Before (value= "pointcut ()") public void beforeMethod (JoinPoint jointPoint) {String methodName = jointPoint.getSignature () .getName (); List args = Arrays.asList (jointPoint.getArgs ()); System.out.println ("Method (" + methodName+ ") args (" + args+ ") Start") } @ After (value= "pointcut ()") public void afterMethod (JoinPoint jointPoint) {String methodName = jointPoint.getSignature () .getName (); List args = Arrays.asList (jointPoint.getArgs ()); System.out.println ("Method (" + methodName+ ") args (" + args+ ") After") } @ AfterReturning (value= "pointcut ()", returning= "result") public void afterReturingMethod (JoinPoint jointPoint,Object result) {String methodName = jointPoint.getSignature () .getName (); List args = Arrays.asList (jointPoint.getArgs ()); System.out.println ("Method (" + methodName+ ") args (" + args+ ") After Runing + Result:" + result) } @ AfterThrowing (value= "pointcut ()", throwing= "e") public void afterThrowingMethod (JoinPoint jointPoint,Exception e) {String methodName = jointPoint.getSignature () .getName (); List args = Arrays.asList (jointPoint.getArgs ()); System.out.println ("Method (" + methodName+ ") args (" + args+ ") AfterThrow Exception:" + e) } @ Around (value= "pointcut ()") public Object aroundMethod (ProceedingJoinPoint jointPoint) {Object result = null; String methodName = jointPoint.getSignature (). GetName (); try {System.out.println (methodName+ "pre-notification."); result = jointPoint.proceed (); System.out.println ("post notification.") } catch (Throwable e) {System.out.println ("exception notification."); throw new RuntimeException ();} System.out.println ("return notification."); return result;}}
Note: @ Around surround notification. Object ProceedingJoinPoint parameter must be carried.
Surround notification is similar to the whole process of a dynamic agent and can decide whether or not to execute the target method.
Priority of section
You can annotate @ Order on the section, where the smaller the value, the higher the priority of the section.
two。 Profile-based approach
The steps are as follows:
Move the above three classes to other packages and remove all comments in the classes and methods
Define a new profile applicationContext-xml.xml
The structure is as follows:
Configuration of applicationContext-xml.xml
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.