首页 > 代码库 > 峰Spring4学习(6)spring AOP的应用例子
峰Spring4学习(6)spring AOP的应用例子
一、AOP简介:
二、AOP实例:
三、使用的例子
需求:在student添加的前后,打印日志信息;
0)spring AOP需要引用的jar包:
1)StudentService.java接口:
package com.cy.service; public interface StudentService { public void addStudent(String name); }
2)StudentServiceImpl.java实现类:
package com.cy.service.impl; import com.cy.service.StudentService; public class StudentServiceImpl implements StudentService { @Override public void addStudent(String name) { System.out.println("添加学生"+name); } }
3)StudentServiceAspect.java切面类:
package com.cy.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(); //retVal为StudentServiceImpl.addStudent(String name)方法的返回值 //因为返回值为void,所以console打印null System.out.println("添加学生后"); System.out.println(retVal); 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()); } }
4)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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="studentService" class="com.cy.service.impl.StudentServiceImpl"></bean> <bean id="studentServiceAspect" class="com.cy.advice.StudentServiceAspect"></bean> <aop:config> <aop:aspect id="studentServiceAspect" ref="studentServiceAspect"> <aop:pointcut expression="execution(* com.cy.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>
5)测试代码:
public class T { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); StudentService studentService = (StudentService) ac.getBean("studentService"); studentService.addStudent("张三"); } }
1)没有异常抛出时,没有异常通知;console打印:
2)addStudent方法中构造异常,有异常通知,console打印:
StudentServiceImpl.java:
package com.cy.service.impl; import com.cy.service.StudentService; public class StudentServiceImpl implements StudentService { @Override public void addStudent(String name) { System.out.println("添加学生"+name); System.out.println(1/0); } }
doAround方法中抛出异常是这句话:
-- Object retVal = pjp.proceed();
峰Spring4学习(6)spring AOP的应用例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。