spring aop注解方式

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:component-scan base-package="org.spring"/>
    <!--开启aop注解的支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@Component
@Aspect
public class Log {
//    切入点
    @Pointcut(value = "execution(public void org.spring.Hello.aop())")
    public void pt() {
    }

    public void before() {
        System.out.println("aop before");
    }

    public void after() {
        System.out.println("aop after");
    }

    public void exp() {
        System.out.println("aop ex");
    }

    public void fina() {
        System.out.println("aop fina");
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Object proceed = null;
        try {
            System.out.println("around前置通知");
            proceed = joinPoint.proceed(args);
            System.out.println("后置");
        } catch (Throwable throwable) {
            System.out.println("异常");

        } finally {
            System.out.println("最终");
        }
        return proceed;
    }

}

推荐环绕通知

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。