Conquer Spring AOP --@AspectJ

Keywords: Spring xml encoding JDK

Links to the original text: https://my.oschina.net/mohaiyong/blog/221283
Conquering Spring AOP-Schema Following the original example, the Schema mode is converted to @AspectJ mode.
Annotation is indeed more concise, from configuration priority to contract priority, or need a point of process, at least understanding and psychological!



Relevant content:
Conquering Spring AOP-Schema
Conquer Spring AOP --@AspectJ



First look at the Spring configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
	<!-- proxy-target-class default"false",Change to"ture"Use CGLib Dynamic Agent -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<bean
		id="hello"
		class="org.zlex.aop.SayHello" />
	<bean class="org.zlex.aop.AspectJAdvice" />
</beans>


< aop: aspectj-autoproxy /> Open the automatic agent. proxy-target-class defaults to "false" using JDK proxy and changes to "ture" using CGLib dynamic proxy.
org.zlex.aop.AspectJAdvice was implemented in the AspectJ annotation of the Advice class in the previous article.
Step by step.
@Aspect Annotate the implementation class of AspectJ:
@Aspect
public class AspectJAdvice {
// ...
}

Let's start with BeforeAdvice, which is implemented as follows:
/**
	 * @param joinPoint
	 */
	@Before("execution(* org.zlex.aop.Hello.sayHelloBefore(..))")
	public void beforeAdvice(JoinPoint joinPoint) {
		System.out.println("Before: " + joinPoint.getSignature().getName());
	}

@ Before("execution(* org.zlex.aop.Hello.sayHelloBefore(.))") can also be written as @Before(value="execution(* org.zlex.aop.Hello.sayHelloBefore(..))").
When executed The beforeAdvice method is triggered when the org.zlex.aop.Hello.sayHelloBefore(.) method is used.

AfterAdvice and Around Advice are similar in code as follows:
/**
	 * After
	 * 
	 * @param joinPoint
	 */
	@After(value = "execution(* org.zlex.aop.Hello.sayHelloAfter(..))")
	public void afterAdvice(JoinPoint joinPoint) {
		System.out.println("After: " + joinPoint.getSignature().getName());
	}

	/**
	 * Around
	 * 
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* org.zlex.aop.Hello.sayHelloAround(..))")
	public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("Around: " + joinPoint.getSignature().getName());
		System.out.println("Before");
		Object obj = joinPoint.proceed();
		System.out.println("End");
		return obj;
	}

It's just that Around Advice here needs to implement the method ProceedingJoinPoint is used as a parameter to execute the surround method and return the execution result.

AfterReturning Advice can retrieve the return value of the target method as follows:
/**
	 * AfterReturning
	 * 
	 * @param joinPoint
	 */
	@AfterReturning(value = "execution(* org.zlex.aop.Hello.sayHelloAfterReturning(..))", returning = "retVal")
	public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {
		System.out.println("AfterReturning: "
				+ joinPoint.getSignature().getName());
		System.out.println("Return Value: " + retVal);
	}

@ In AfterReturning (value = execution (* org.zlex.aop.Hello.sayHelloAfterReturning (.)), returning = retVal), there is a comment parameter. returning is used to identify the return value parameter, which is consistent with the parameter name in the method.

AfterThrowing Advice is similar to AfterReturning Advice in that it obtains the exception thrown by the target object, which is coded as follows:
/**
	 * AfterThrowing
	 * 
	 * @param joinPoint
	 */
	@AfterThrowing(value = "execution(* org.zlex.aop.Hello.sayHelloAfterThrowing(..))", throwing = "e")
	public void afterThrowingAdvice(JoinPoint joinPoint, Exception e) {
		System.out.println("AfterThrowing: "
				+ joinPoint.getSignature().getName());
		System.out.println("Exception Message: " + e.getMessage());
	}


The annotation implementation of Introduction is emphasized here. The code is as follows:
@DeclareParents(value = "org.zlex.aop.SayHello", defaultImpl = org.zlex.aop.IntroductionOk.class)
	public Ok ok;

The Ok interface here is used to extend the original SayHello. This member variable (ok) must be marked public, that is, public Okok;.
@ In the DeclareParents parameter, defaultImpl pointing The implementation class of Ok interface, value identifies the target class. Compared with Schema's complex configuration, it is one step in place!  

There's no more bullshit here. Code meets Enclosure!



Relevant content:
Conquering Spring AOP-Schema
Conquer Spring AOP --@AspectJ

Reproduced in: https://my.oschina.net/mohaiyong/blog/221283

Posted by goatboy on Sun, 08 Sep 2019 22:28:50 -0700