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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章