SpringAop小例

面向切面编程(也叫面向方面编程)Aspect Oriented Programming(AOP)
Spring AOP  实例  
1 ,前置通知;
2 ,后置通知;
3 ,环绕通知;
4 ,返回通知;
5 ,异常通知; 
——————————————————————————————————————
  导入JAR包
——————————————————————————————————————
   

——————————————————————————————————————
beans.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
 
<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
 <aop:config>
 <aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
                <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
 <aop:before method="doBefore" pointcut-ref="businessService"/>
 <aop:after method="doAfter" pointcut-ref="businessService"/>
 <aop:around method="doAround" pointcut-ref="businessService"/>
 <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
 <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
 </aop:aspect> 
 </aop:config>
</beans>


——————————————————————————————————————
package com.java1234.service.impl;
import com.java1234.service.StudentService;
public class StudentServiceImpl implements StudentService{

 @Override
 public void addStudent(String name) {
 // System.out.println("开始添加学生"+name);
 System.out.println("添加学生"+name);
 //System.out.println(1/0);
 // System.out.println("完成学生"+name+"的添加");
 }
}
——————————————————————————————————————
package com.java1234.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class StudentServiceAspect {

 public void doBefore(JoinPoint jp){
 System.out.println("类名:"+jp.getTarget().getClass().getName());
 System.out.println("方法名:"+jp.getSignature().getName());
 System.out.println("开始添加学生:"+jp.getArgs()[0]);
 }
 
 public void doAfter(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) throws Throwable{
 System.out.println("添加学生前");
 Object retVal=pjp.proceed();
 System.out.println(retVal);
 System.out.println("添加学生后");
 return retVal;
 }
 
 public void doAfterReturning(JoinPoint jp){
 System.out.println("返回通知");
 }
 
 public void doAfterThrowing(JoinPoint jp,Throwable ex){
 System.out.println("异常通知");
 System.out.println("异常信息:"+ex.getMessage());
 }
}


——————————————————————————————————————
执行顺序
前置通知
环绕通知(前置通知)
返回通知
环绕通知(后置通知)
后置通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值