美文网首页
spring aop注解方式

spring aop注解方式

作者: simplerandom | 来源:发表于2020-07-24 10:23 被阅读0次
<?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;
    }

}

推荐环绕通知

相关文章

网友评论

      本文标题:spring aop注解方式

      本文链接:https://www.haomeiwen.com/subject/lffjlktx.html