Spring Aop实例

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PackingSome {

}
import com.example33.demo.model.UserModel;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SimpleAspect {

    private static final Logger logger = LoggerFactory.getLogger(SimpleAspect.class);

    @Pointcut("execution(public * com.example33.demo.service.*.*(..)) && @annotation(com.example33.demo.aop.PackingSome)")
    public void pointCut() {
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("after aspect executed");
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        //如果需要这里可以取出参数进行处理
//        Object[] args = joinPoint.getArgs();
        System.out.println("before aspect executing");
    }

    @AfterReturning(pointcut = "pointCut()", returning = "returnVal")
    public Object afterReturning(JoinPoint joinPoint, Object returnVal) {
        if (!(returnVal instanceof UserModel)) {
            logger.info("没有继承UserModel"+returnVal.getClass());
            return returnVal;
        }
        /**
         * 补全user结果集
         */
        UserModel user = (UserModel)returnVal;
        user.setChinese(99L);
        user.setMath(67L);
        user.setEnglish(98L);
        user.setRank(2L);
        System.out.println("afterReturning executed, return result is "
                + returnVal);
        return user;
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around start..");
        /**
         * 这里一定要返回值,不然前端拿不到结果
         */
        Object retVal;
        try {
            retVal = pjp.proceed();
        } catch (Throwable ex) {
            System.out.println("error in around");
            throw ex;
        }
        System.out.println("around end");
        return retVal;
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "error")
    public void afterThrowing(JoinPoint jp, Throwable error) {
        System.out.println("error:" + error);
    }
import com.example33.demo.model.UserModel;
import com.example33.demo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("aop/test")
public class TestAopController {
    private final UserService userService;

    public TestAopController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/findUser")
    public UserModel findUser(Long id) {
        return userService.findUser(id);
    }
}
import com.example33.demo.aop.PackingSome;
import com.example33.demo.entity.User;
import com.example33.demo.mapper.UserMapper;
import com.example33.demo.model.UserModel;
import com.example33.demo.utils.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @PackingSome
    public UserModel findUser(Long id) {
        User user = userMapper.selectById(id);
        UserModel model = BeanUtils.map(user, UserModel.class);
        return model;
    }
}

你可能感兴趣的:(Spring Aop实例)