Spring Introduction Study Notes (3.05) -- reuse entry point definition

Source: Internet
Author: User
Tags log log

I. knowledge points

When writing aspectj aspect, You can embed the entry point expression in the notification annotation directly. However, the same entry point expression may be repeated in multiple notifications.

Like many other AOP implementations, aspectj allows you to define the entry point independently and reuse it in multiple notifications.

Ii. Sample Code

In aspectj aspect, the entry point can be declared as a simple method with @ pointcut annotation. The method body of the entry point is usually empty, because it is unreasonable to mix the entry point definition with the application logic. The access modifier of the Entry Method controls the visibility of the entry. Other notifications can reference this entry point with the method name.

 

package com.codeproject.jackie.springrecipesnote.springaop;import java.util.Arrays;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;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;/** * @author jackie *  */@Aspectpublic class CalculatorLoggingAspect {private Log log = LogFactory.getLog(this.getClass());@Pointcut("execution(* *.*(..))")private void loggingOperation() {}@Before("loggingOperation()")public void logBefore(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));}@After("loggingOperation()")public void logAfter(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName() + "() ends");}@AfterReturning(pointcut = "loggingOperation())", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);}@AfterThrowing(pointcut = "loggingOperation())", throwing = "e")public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");}@Around("loggingOperation()")public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));try {Object result = joinPoint.proceed();log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);return result;} catch (IllegalArgumentException e) {log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");throw e;}}}

 

Generally, if the entry point is shared among multiple aspect instances, it is best to centralize them into a public class. In this case, they must be declared as public.

 

package com.codeproject.jackie.springrecipesnote.springaop;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;/** * @author jackie *  */@Aspectpublic class CalculatorPointcuts {@Pointcut("execution(* *.*(..))")public void loggingOperation() {}}

 

This entry point must contain the class name. If the class is not in the same package as the aspect, the package name must also be included.

 

        @Before("CalculatorPointcuts.loggingOperation()")public void logBefore(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));}@After("CalculatorPointcuts.loggingOperation()")public void logAfter(JoinPoint joinPoint) {log.info("The method " + joinPoint.getSignature().getName() + "() ends");}@AfterReturning(pointcut = "CalculatorPointcuts.loggingOperation())", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);}@AfterThrowing(pointcut = "CalculatorPointcuts.loggingOperation())", throwing = "e")public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");}@Around("CalculatorPointcuts.loggingOperation()")public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));try {Object result = joinPoint.proceed();log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);return result;} catch (IllegalArgumentException e) {log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");throw e;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.