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
- 編寫一個普通類
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>