[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第七讲 源码

[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第八讲 源码

配置:以下重点划出的是新添加进去的

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
"
>

、、切面配置如下

    <beanid="studentServiceAspect"class="com.java1234.advice.StudentServiceAspect"></bean>//先创建好两个bean<beanid="studentService"class="com.java1234.service.impl.StudentServiceImpl"></bean>
    
    <aop:config>//config  aop标志<aop:aspectid="studentServiceAspect"ref="studentServiceAspect">//aspect定义一个切面,studentServiceAspect是切面类
//定义切点,是方法级别的,要写表达式{第一个*表示任何是任意,后面的两点..表示参数是任意的},指定方法
<aop:pointcutexpression="execution(* com.java1234.service.*.*(..))"id="businessService"/> <aop:beforemethod="doBefore"pointcut-ref="businessService"/> <aop:aftermethod="doAfter"pointcut-ref="businessService"/> <aop:aroundmethod="doAround"pointcut-ref="businessService"/>//环绕<aop:after-returningmethod="doAfterReturning"pointcut-ref="businessService"/>//返回<aop:after-throwingmethod="doAfterThrowing"pointcut-ref="businessService"throwing="ex"/>//异常</aop:aspect> </aop:config>

packagecom.java1234.advice;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;public classStudentServiceAspect {public voiddoBefore(JoinPoint jp){
System.out.println(
"类名:"+jp.getTarget().getClass().getName());
System.out.println(
"方法名:"+jp.getSignature().getName());
System.out.println(
"开始添加学生:"+jp.getArgs()[0]);
}
public voiddoAfter(JoinPoint jp){
System.out.println(
"类名:"+jp.getTarget().getClass().getName());
System.out.println(
"方法名:"+jp.getSignature().getName());
System.out.println(
"学生添加完成:"+jp.getArgs()[0]);
}
public Object doAround(ProceedingJoinPoint pjp) throwsThrowable{
System.out.println(
"添加学生前");
Object retVal
=pjp.proceed();
System.out.println(retVal);
System.out.println(
"添加学生后");returnretVal;
}
public voiddoAfterReturning(JoinPoint jp){
System.out.println(
"返回通知");
}
public voiddoAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println(
"异常通知");
System.out.println(
"异常信息:"+ex.getMessage());
}
}

标签: none

添加新评论