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>