Spring AOP
通过XML注释使用AOP
参考以下
1:添加aspectj的支持
1 <dependency> 2 <groupId>org.aspectj</groupId> 3 <artifactId>aspectjrt</artifactId> 4 <version>1.8.1</version> 5 </dependency> 6 <dependency> 7 <groupId>org.aspectj</groupId> 8 <artifactId>aspectjweaver</artifactId> 9 <version>1.8.1</version> 10 </dependency>
2:UserDao类
1 public interface UserDao { 2 public String findUserById(int id); 3 } 4 5 public class UserDaoImpl implements UserDao { 6 public String findUserById(int id) { 7 if(id < 0) { 8 throw new IllegalArgumentException("用户id不能小于0"); 9 } 10 return "zang"; 11 } 12 }
3:XMLAspect
1 public class XMLAspect { 2 private void doBefore(JoinPoint joinPoint) { 3 System.out.println("-----doBefore().invoke-----"); 4 System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等"); 5 System.out.println(" 可通过joinPoint来获取所需要的内容"); 6 System.out.println("-----End of doBefore()------"); 7 } 8 9 private Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { 10 System.out.println("-----doAround().invoke-----"); 11 System.out.println(" 此处可以做类似于Before Advice的事情"); 12 13 //调用核心逻辑 14 Object retVal = joinPoint.proceed(); 15 16 System.out.println(" 此处可以做类似于After Advice的事情"); 17 System.out.println("-----End of doAround()------"); 18 return retVal; 19 } 20 21 private void doAfter(JoinPoint joinPoint) { 22 System.out.println("-----doAfter().invoke-----"); 23 System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等"); 24 System.out.println(" 可通过joinPoint来获取所需要的内容"); 25 System.out.println("-----End of doAfter()------"); 26 } 27 28 private void doAfterReturning(JoinPoint joinPoint) { 29 System.out.println("-----doReturn().invoke-----"); 30 System.out.println(" 此处可以对返回值做进一步处理"); 31 System.out.println(" 可通过joinPoint来获取所需要的内容"); 32 System.out.println("-----End of doReturn()------"); 33 } 34 35 private void doAfterThrowing(JoinPoint joinPoint,Throwable ex) { 36 System.out.println("-----doThrowing().invoke-----"); 37 System.out.println(" 错误信息:"+ex.getMessage()); 38 System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等"); 39 System.out.println(" 可通过joinPoint来获取所需要的内容"); 40 System.out.println("-----End of doThrowing()------"); 41 } 42 }
4:applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 11 12 <context:component-scan base-package="test"/> 13 14 <bean id="userDao" class="dao.UserDaoImpl"></bean> 15 16 <bean id="xmlAspect" class="aspect.XMLAspect"></bean> 17 18 <aop:config> 19 <aop:aspect id="aspect" ref="xmlAspect"> 20 <!-- 定义切入点,一个切入点可能横切多个业务组件 --> 21 <aop:pointcut expression="execution(* dao.*.find*(..))" id="pointcut"/> 22 23 <!-- 定义切入点上的增强处理 --> 24 <aop:before method="doBefore" pointcut-ref="pointcut"/> 25 <aop:around method="doAround" pointcut-ref="pointcut"/> 26 <aop:after method="doAfter" pointcut-ref="pointcut"/> 27 <aop:after-returning method="doAfterReturning" pointcut-ref="pointcut"/> 28 <aop:after-throwing method="doAfterThrowing" pointcut-ref="pointcut" throwing="ex"/> 29 30 </aop:aspect> 31 </aop:config> 32 33 34 </beans>
其中execution的规则如下图
5:测试代码
1 public class SpringTest { 2 public static void main(String[] args) { 3 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 4 5 UserDao userDao = (UserDao)ac.getBean("userDao"); 6 7 userDao.findUserById(1); 8 9 System.out.println("=================="); 10 11 try { 12 // 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获 13 userDao.findUserById(-1); 14 } catch (IllegalArgumentException e) { 15 } 16 } 17 }
结果
通过注解使用AOP
参考以下两篇博文
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。