A list,
Spring AOP provides a comprehensive AOP solution. Spring seamlessly integrates Spring AOP and IOC with AspectJ to meet all of AOP’s usage requirements in a consistent Spring-based application architecture.
SpringBoot integrates Spring-AOP
Add the dependent
Maven dependencies are added as follows <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! > <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-AOP </artifactId> </dependency>Copy the code
Note: No additional configuration is required after the introduction of AOP dependencies is complete. The spring. Aop. auto attribute is enabled by default, meaning that @enableAspectJAutoProxy has been added by default whenever AOP dependencies are introduced. You do not need to add @EnableAspectJAutoProxy to the program main class to enable this.
Write aspect code
The Aspect class uses the @aspect annotation and must add @Component to inject the Aspect class into IOC container management
@Aspect @Component @Slf4j public class TestLog { @Pointcut("execution(* com.yuntian.example.controller.*.*(..) @param jp */ @before ("testLog()") public void doBefore(JoinPoint JP) {/ /... Log.info (" Before method execution :............." ); } @param jp */ @after ("testLog()") public void doAfter(JoinPoint JP) {//... Log.info (" After method execution :............." ); @afterreturning ("testLog()") public void doAfterReturning(JoinPoint jp) {// . Log.info (" Return notification after method execution :............." ); * @param jp * @param e */ @afterthrowing (value="testLog()",throwing="e") public void doAfterThrowing(JoinPoint jp,Exception e) { // ... Log.info (" Exception after method execution :............." ); }}Copy the code
Business code
package com.yuntian.example.controller; import com.yuntian.example.base.Result; import com.yuntian.example.entity.dto.OrderDTO; import com.yuntian.example.entity.vo.OrderVO; import com.yuntian.example.service.OrderService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author yuntian. * @date Created in 18:13 2019/8/7 * @description */ @RestController @RequestMapping("order") public class OrderController { @Resource private OrderService orderService; @PostMapping("/createOrder") public Result createOrder(OrderDTO dto){ OrderVO orderVO= orderService.createOrder(dto); Result<OrderVO> result=new Result<>(); result.setData(orderVO); result.setCode(99); return result; }}Copy the code
The results of
You can see faceted classes that implement logic without intruding into the business code.
Write aspect code, using surround notification
@Aspect @Component @Slf4j public class TestLogAround { @Pointcut("execution(* com.yuntian.example.controller.*.*(..) )") public void testLogAround() {} /** * use the surround notification * @param PJP * @return */ @around ("testLogAround()") public Object doAround(ProceedingJoinPoint pjp){ Object obj=null; Try {log.info(" pre-notification: before execution "); obj=pjp.proceed(); Log.info (" Return notification: after execution "); } catch(Throwable e){log.info(" Exception notification:........ !" ); } return obj; }}Copy the code
The results of
Write aspect code with custom annotations
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MTransaction { @AliasFor("transactionManager") String value() default ""; @AliasFor("value") String transactionManager() default ""; int timeout() default -1; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {}; } @Component @Slf4j @Aspect public class TransactionAspect { The @pointcut (" @ the annotation (com) yuntian. Example. The annotation. MTransaction) ") public void transactionAspect () {} / * * * * use around the notice * @param PJP * @return */ @around ("transactionAspect()") public Object doAround(ProceedingJoinPoint PJP) MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); Method method = methodSignature.getMethod(); Object[] args= pjp.getArgs(); Object obj = null; Try {log.info(" Pre-notification: start transaction...") ); MTransaction transaction = method.getAnnotation(MTransaction.class); if (transaction ! = null) {log.info(" transaction: "+ transaction. Value ()); } log.info(" argument: "+ json.tojsonString (args)); obj = pjp.proceed(); Log.info (" Return notification: close transaction..." ); } catch (Throwable e) {log.info(" exception notification: transaction rollback..." ); } return obj; }}Copy the code
The results of
Two, section introduction
Tangent expression
Pointcuts are methods of all classes in the scope
@Pointcut("execution(* com.yuntian.example.controller.*.*(..) )"Copy the code
Pointcuts are annotated methods
@Pointcut("@annotation(com.yuntian.example.annotation.MTransaction)")
Copy the code
AspectJ indicator
indicator | describe |
---|---|
arg() | Limits the join point matching parameter to the execution method of the specified type |
@args() | Limits the join point matching parameter to the execution method of the specified annotation annotation |
execution() | The execution method used to match is the join point |
this() | Limits the join point matching parameter to the execution method of the specified annotation annotation |
target | Restrict join points to match classes whose target objects are of the specified type |
within() | Restricts the join point to match the specified type |
@within() | Restrict join points to match the type of the specified annotation lock annotation (when using Spring Aop, the method defines the class annotated by the specified annotation) |
@annotation | Restricts matching join points with specified annotations |
JoinPoint usage
Iv. Reference materials
Blog.csdn.net/XU906722/ar…