Spring--Aop-AspectJ

AspectJ:
AspectJ is an AOP framework based on the Java language

jar包:
 AOP联盟:com.springsource.org.aopalliance-1.0.0.jar
 spring aop实现:spring-aop-3.2.0.RELEASE.jar
 Aspectj规范:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
 spring aspect实现:spring-aspects-3.2.0.RELEASE.jar

 Pointcut expression:
1.execution() is used to describe the method (the usage is introduced in spring--aop)
2.within() is used to describe the method in the package, matching package or sub-package (understand)
3.this () is used to describe the current object and matches the methods in the proxy object that implements the interface (understand)
4.target() is used to describe the target object and matches the methods in the target class that implements the interface (understand)
5.args() is used for Describe the parameters, match the method with the parameter format conforming to the standard (understand)
6. bean() is used to describe the specified bean

Notification type: aspectj
notification type: pre-notification, post-notification, surround notification, exception throw notification, final  notification Exception, preventing the target method from running 2) afterReturning: post-notification (application: general data processing)  method is executed after normal return, if an exception is thrown in the target method, the notification cannot be executed and  must be executed after the method is executed, so the method's return value. 3) around: surround notification (application: very powerful, can do anything) [must be Object, must have the first parameter, its parameter is the sub-interface ProceedingJoinPoint joinPoint of JoinPoint, an exception must be thrown externally, manually Execution target method joinPoint.proceed()]  The method is executed before and after execution, which can prevent the execution of the method 4) afterThrowing: throw exception notification (application: wrapping exception information, recording log (info, warn, error), etc.)  method throws exception After execution, if the method does not throw an exception, it cannot be executed 5) after: final notification (application: clean up the scene)  The method is executed after the execution is completed, regardless of whether there is an exception in the method. Surround try{    //Pre-notification    manually executes the target method Object obj = ...    //post notification } catch{    //throw exception notification } finally{





















   // final notification
}

Notification demo XML configuration:
public class MyAspect {
 
 public void myBefore(JoinPoint joinPoint){
  System.out.println("Pre-notice:" + joinPoint.getSignature().getName());
 }
 public void myAfterReturning(JoinPoint joinPoint,Object val){
  System.out.println("Post notification:" + joinPoint.getSignature().getName() + " @ " + val);
 }
 
 public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
  System.out.println( "Before wrapping");
  //Manually execute the target method
  Object obj = joinPoint.proceed();
  
  System.out.println("After wrapping");
  return obj;
 }
 
 public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
  System. out.println("Throw exception notification" + e.getMessage());
 }
 
 public void myAfter(JoinPoint joinPoint){
  System.out.println("Final notification");
 }
}
<!-- 1 Create service -->
 <bean id="userServiceId" class="cn.xxx.d_aspect.a_xml. UserServiceImpl"></bean>
 <!-- 2 aspect classes-->
 <bean id="myAspectId" class="cn.xxx.d_aspect.a_xml.MyAspect"></bean>
 <!-- 3 aop aspectj programming
  <aop:aspect> for aspect configuration, aspect configuration
   *ref is used to refer to the aspect class to determine the advice (method name)
  <aop:pointcut> declares the pointcut
 -->
 <aop:config>
  <aop:aspect ref="myAspectId ">
   <aop:pointcut expression="execution(* cn.xxx.d_aspect.a_xml.*.*(..))" id="myPointcut"/>
   <!-- 3.1 前置通知
    * 配置:<aop:before method="myBefore" pointcut-ref="myPointcut"/>
    * Notice: public void myBefore(JoinPoint joinPoint){
     has one parameter to get the description information of the current join point. For example: method name joinPoint.getSignature().getName()
     type: org.aspectj.lang.JoinPoint
   <aop:before method="myBefore" pointcut-ref="myPointcut"/>
   -->
   <!-- 3.2 Postfix Notification, executed after the target method, so you can get the return value of the target method
    * Configuration: <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="val"/>
    * Notification: public void myAfterReturning( JoinPoint joinPoint,Object val){
     parameter 1: description of the current join point
     parameter 2: return value, the type must be Object, and the parameter name must be set by <aop:after-returning ...returning="
">.
   <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="val"


    * Configuration: <aop:around method="myAround" pointcut-ref="myPointcut" />
    * Notification: public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
     1) The return type must be Object
     2) The method parameter must be ProceedingJoinPoint
     3) Throwable exception must be thrown
     4) Execute the target method: Object obj = joinPoint.proceed();
   <aop:around method="myAround" pointcut-ref="myPointcut" />
   -->
   <!-- 3.4 Throwing an exception, Executed only when an exception occurs. Usually not implemented.
    * Configuration: <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/>
    * Notification: public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
     parameter 1:. . .
     Parameter 2: The description of the exception, the type must be Throwable, and the parameter name is determined by the throwing configuration
   <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/>
   -->
   <!-- 3.5 最终通知
    * 配置: <aop:after method="myAfter" pointcut-ref="myPointcut"/>
    * 通知:public void myAfter(JoinPoint joinPoint){
   -->
   <aop:after method="myAfter" pointcut-ref="myPointcut"/>
  </aop:aspect>
 </aop:config>

Annotation configuration:
@service declares the service layer, and annotates @service ("userService") on the implementation layer of the service.
@Component declares poorly categorized components. For example: @Component("myAspectId") The
service and component can take effect only by scanning the context.

@Aspect declares the aspect @Before Front @AfterReturning Post @Around Surrounds
@AfterThrowing Throws an exception @After Finally
@Pointcut declares the entry point, decorates the fixed method private void Custom (){}
For the above annotations to take effect, they must be configured in xml
<aop:aspectj-autoproxy>

Pre-notification: @Before("execution (* cn.xxx.d_aspect.b_annotation.*.*(..))")
Post-notification: @AfterReturning(value="myPointCut ()" ,returning="val")
   Declare pointcuts, reach indicates sharing. When used, it is equivalent to a method call
    @Pointcut("execution(* cn.xxx.d_aspect.b_annotation.*.*(..))")
    private void myPointCut(){} [declared in the aspect class]

Surround notification: @Around("execution (* cn.xxx.d_aspect.b_annotation.*.*(..))")
throw exception notification: @AfterThrowing(value="myPointCut ()" ,throwing="e")


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326555909&siteId=291194637