`
cywhoyi
  • 浏览: 413381 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

AOP的切面思考

    博客分类:
  • JAVA
阅读更多

简单介绍下如何使用PointCut、Advisor

 

@Component
@Aspect
public class LogAspect {

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Autowired
    TestService testService;

    @Pointcut("execution(* com.aop.service.UserService.add*(..))&& args(userDto)&& !within(com.aop.service.UserService)")
    public void getPointcutName(UserDto userDto) {
    }

    @After("getPointcutName(userDto)")
    public void logBeforePointCut(JoinPoint joinPoint, UserDto userDto) throws Throwable {
        log.info("logBeforePointCut() is running!");
        log.info("method: " + joinPoint.getSignature().getName());
        log.info("params: " + Arrays.toString(joinPoint.getArgs()));

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());
    }

    @Before("execution(* com.aop.service.UserService.addUserBefore(..)) && args(userDto)")
    public void logBefore(JoinPoint joinPoint, UserDto userDto) throws Throwable {
        log.info("logBefore() is running!");
        log.info("method: " + joinPoint.getSignature().getName());
        log.info("params: " + Arrays.toString(joinPoint.getArgs()));

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());
    }

    @After("execution(* com.aop.service.UserService.addUserAfter(..)) && args(userDto)")
    public void logAfter(JoinPoint joinPoint, UserDto userDto) throws Throwable {
        log.info("logAfter() is running!");
        log.info("method: " + joinPoint.getSignature().getName());
        log.info("params: " + Arrays.toString(joinPoint.getArgs()));

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());
    }

    @AfterReturning(
            pointcut = "execution(* com.aop.service.UserService.addUserAfterReturning(..))",
            returning = "userDto")
    public void logAfterReturning(JoinPoint joinPoint, UserDto userDto) {
        log.info("logAfterReturning() is running!");
        log.info("method: " + joinPoint.getSignature().getName());

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());
    }

    @Around("execution(* com.aop.service.UserService.addUserAround(..)) && args(userDto)")
    public void logAround(ProceedingJoinPoint joinPoint, UserDto userDto) throws Throwable {
        log.info("logAround() is running!");
        log.info("method: " + joinPoint.getSignature().getName());
        log.info("params: " + Arrays.toString(joinPoint.getArgs()));

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());

        log.info("around before is running!");

        joinPoint.proceed();

        log.info("around after is running");
    }

    @Before("execution(* com.aop.service.UserService.addUserBeforeVoid(..)) && args(userDto)")
    public void logBeforeVoid(JoinPoint joinPoint, UserDto userDto) throws Throwable {
        log.info("logBeforeVoid() is running!");
        log.info("method: " + joinPoint.getSignature().getName());

        log.info("username: " + userDto.getUsername());
        log.info("email: " + userDto.getEmail());

        testService.testService();
    }
}

 

public interface UserService {
	
	UserDto addUserBefore(UserDto userDto);
	UserDto addUserAfter(UserDto userDto);
	UserDto addUserAfterReturning(UserDto userDto);
	UserDto addUserAround(UserDto userDto);
	void addUserBeforeVoid(UserDto userDto);
}

 

Advisor提供了before、after、around、afterreturning,具体用处的场景,我简单罗列下

1.日志=》追踪问题

2.运行期间加强类的额外功能,注入拦截,鉴权...

 

额外这里谈下pointCut结合advice,两者主要应用场景有类似之处,pointCut可能更应用在多个复杂条件,advice更为注重其方法行为,通过注解名字before、after等能够一眼感觉出来,那么两者结合后,即把行为+多场景同时结合起来。

 

总结:

spring提供aop后,基本上其它框架只要实现其体现,想做什么就能够为所欲为

 

1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics