AspectJ under AOP in Spring is used based on annotation configuration

1. Guide package: Spring-Aspects – 4.3.18.release. jar (This package will be downloaded automatically when the new IDEA is created for spring project. If there is no package, you need to download it.

2. Configuration:

PS: To use annotations, simply configure aop annotations to work


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <! -- Open comments -->
    <context:annotation-config/>
    <! -- Comment location -->
    <context:component-scan base-package="com.hwt"></context:component-scan>

    <! Configure AOP annotations to work -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>
Copy the code

 

3. Section class (enhancement class) :

PS: This method does not inherit classes

package com.hwt.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component("myAspect3")
@Aspect
public class MyAspect3 {
    // Declare a common pointcut
    @Pointcut("execution(* com.hwt.service.*.*(..) )"
    public  void myPointcut(a){};
    @Before("myPointcut()")
    public void myBefore(a){
        System.out.println("MyBefore");
    }
    @After("myPointcut()")
    public void myAfter(a){
        System.out.println("MyAfter final notice...");
    }
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint joinPoint,Object retValue) throws Throwable {
        System.out.println("MyAfterReturning notification + Return value..."+retValue);
    }
    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("MyAround surround notification...");
        System.out.println("MyAround opens transaction...");
        System.out.println("MyAround closes transaction...");
        proceedingJoinPoint.proceed();
        return proceedingJoinPoint.proceed();
    }
    @AfterThrowing(pointcut = "myPointcut()",throwing = "throwable")
    public void myAfterThrowing(JoinPoint joinPoint,Throwable throwable) throws Throwable {
        System.out.println("MyAfterThrowing exception notification + exception..."+ throwable.getMessage()); }}Copy the code

4. Original Business Category:

package com.hwt.service;

import org.springframework.stereotype.Service;

@Service("studentService")
public class StudentService {
    public void delete(a){
        System.out.println("Delete students!");
    }
    public void add(a){
        System.out.println("Add students!");
    }
    public void update(a){
        System.out.println("Modify the message!"); }}Copy the code

5. Test classes:

package com.hwt.test;

import com.hwt.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test05 {
    @Test
    public void test01(a){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans05.xml");

        StudentService studentService = (StudentService) context.getBean("studentService");

        studentService.add();
// studentService.delete();
// studentService.update();}}Copy the code