切入点表达是:可以使用通配符
*:表示当前层任意
<aop:after method="fn" pointcut="execution(public void com.array.aop.base.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution(public * com.array.aop.base.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution(* void com.array.aop.base.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution(* com.*.aop.base.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution(* com.*.*.base.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution* *..*.Mytarget.save1())"/>
<aop:after method="fn" pointcut="execution(* *..*())"/>
<aop:after method="fn" pointcut="execution(* *..*(*))"/><aop:after method="fn" pointcut="execution(* *..*(*,*))"/>
<aop:after method="fn" pointcut="execution(* *..*(*,..))"/>
这个表示任意返回值,任意包下,任意参数,适用于所有通知类中的方法
<aop:after method="fn" pointcut="execution(* *..*(..))"/>
代码实现:
1.目标对象
public class MyTarget2 {
public int save(int a,String b) {
//int i = 1/0;
System.out.println("save....."+a+","+b);
return 100;
}
}
2.通知
public class MyAdvice2 {
public void before(JoinPoint jp) {
Object[] params = jp.getArgs();
System.out.println("before"+params[0]+params[1]);
}
//before与before1的方法是一样的都是可以获取传入save的参数,before1的方法中也可以不传入JoinPoint jp参数如果传入必须放在第一的位置上
//参数a,b 必须和配置文件中args()中传入的参数名字一致(指定方法中的形参名)
public void before1(JoinPoint jp,int a,String b) {
System.out.println(a+","+b);
}
public void after(JoinPoint jp) {
Object[] params = jp.getArgs();
System.out.println("after"+params[0]+params[1]);
}
public void afterReturning(JoinPoint jp,int ab) {
Object[] params = jp.getArgs();
System.out.println("afterReturning"+params[0]+params[1]+ab);
}
public void afterThrowing(JoinPoint jp,Throwable e) {
Object[] params = jp.getArgs();
System.out.println(e);
System.out.println("afterThrowing"+params[0]+params[1]);
}
public Object round1(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("round before");
Object[] param = new Object[]{new Integer(21),"object"} ;
Object jt = pjp.proceed(param);//好方法可以偷偷改变传入的值
System.out.println("round after"+jt);//这里的jt是save方法的返回值100
return new Integer(10);//改变save的返回值
}
/*public void round(ProceedingJoinPoint pjp,String ab) throws Throwable {
System.out.println("round before");
Object[] param = new Object[]{new Integer(21),"object"} ;
pjp.proceed(param);//好方法可以偷偷改变传入的值
System.out.println("round after");
}*/
}
3.xml文件配置:
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.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">
<!--spring管理资源都是bean的形式 -->
<!-- 你的资源是那个类的对象
<bean class="类名" id="类名对应的唯一标识"></bean>
-->
<!--配置aop,切入点与通知之间的关系 _切面 -->
<!--aop:config:配置aop -->
<aop:config>
<aop:pointcut expression="execution(* *..*(..))" id="pt"/>
<aop:aspect ref="myAdvice2">
<!--配置aop的通知类别
aop:before method="" 通知类别的具体通知
aop:before pointcut=""切入点
<aop:before method="fn1" pointcut="执行到save方法时"/>
-->
<!-- 在方法save的前面加入方法fn -->
<aop:before method="before1" pointcut="execution(* *..*(..)) && args(a,b)"/>
<!-- <aop:after method="after" pointcut-ref="pt"/> -->
<!-- <aop:after-returning method="afterReturning" returning="ab" pointcut-ref="pt"/> -->
<!-- <aop:around method="round" pointcut-ref="pt"/> -->
<!-- <aop:around method="round1" pointcut="execution(* com.array.aop.params.MyTarget2.save(..))"/> -->
<!-- <aop:after-throwing throwing="e" method="afterThrowing" pointcut-ref="pt"/> -->
</aop:aspect>
</aop:config>
<!--/将目标对象与通知定义为bean -->
<bean id="myAdvice2" class="com.array.aop.params.MyAdvice2"></bean>
<bean id="myTarget2" class="com.array.aop.params.MyTarget2"></bean>
</beans>
4.测试方法:
public class App2 {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("application4.xml");
MyTarget2 target2 = (MyTarget2) act.getBean("myTarget2");
//int i = 1/0;
int end = target2.save(1,"array");
System.out.println("end="+end);
}
}