Spring入门(4): 注解方式和schema方式实现AOP

JoinPoint jp 获取参数信息,用于注解和schema方式

注解方式实现AOP

自动获取注解:<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

//jp可以获得所有参数

@Aspect

@Component("annotation")

public class LogAnnotation {

    @Before("execution(public * com.service.StudentServiceImpl.addStudent(..))")

    public void myBefore(JoinPoint jp) {

        System.out.println("注解:前置通知。。。");

        

    }

    @AfterReturning("execution(public * com.service.StudentServiceImpl.addStudent(..))")

    public void myAfter(JoinPoint jp) {

        System.out.println("注解:后置通知。。。");

    }

}

schema方式实现AOP

  1. 编写一个普通类

public class LogSchema {

    @Before("execution(public * com.service.StudentServiceImpl.addStudent(..))")

    public void before(JoinPoint jp) {

        System.out.println(">>>>>>>>>>>>>>>>>>>schema 前置通知。。。");

        System.out.println("target:"+jp.getTarget()+" Args:"+jp.getArgs());

 

    }

    @AfterReturning("execution(public * com.service.StudentServiceImpl.addStudent(..))")

    public void after(JoinPoint jp) {

        System.out.println(">>>>>>>>>>>>>>>>>>>schema 后置通知。。。");

    }

}

1. 配置该类,使之成为一个通知类

 <!--schema方式-->

    <aop:config>

        <aop:pointcut id="pcSchema" expression="execution(public void com.service.StudentServiceImpl.addStudent(com.dao.Student))"></aop:pointcut>

        <aop:aspect ref="logSchema">

            <aop:before method="before" pointcut-ref="pcSchema"></aop:before>

            <aop:after-returning method="after" pointcut-ref="pcSchema"></aop:after-returning>

        </aop:aspect>

    </aop:config>

2. 将该类加入IOC容器:<bean id=""…./>

<bean id="logSchema" class="com.aop.LogSchema"></bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值