Spring Aop切面基于Xml的学习
2017-08-18 16:33
337 查看
这是基于上面一篇博客基于注解的修改的,然而我只是把注解给注释了,主要看xml的配置,可以对比一下前面一篇
package com.text2; public interface C1 { public int jia(int a, int b); public int jian(int a, int b); public int chen(int a, int b); public int chu(int a, int b); }
package com.text2; import org.springframework.stereotype.Component; //@Component("C1")/*声明改类为Spring管理的*/ public class C1Impl implements C1 { public C1Impl() { System.out.println("C1Impl构造器"); } public int jia(int a, int b) { return a+b; } public int jian(int a, int b) { return a-b; } public int chen(int a, int b) { return a*b; } public int chu(int a, int b) { return a/b; } }
package com.text2; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; /** * 将该类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面 @aspect * 使用Order声明切面的优先级,值越小优先级越高 */ //@Order(1) //@Aspect/*声明为切面*/ //@Component/*声明为spring管理的组件*/ public class C2 { /** * 定义一个方法,用于声明切入点表达式,该方法中不需要添入其他的代码 * 使用@Pointcut来声明切入点表达式 * 后面的其他通知直接使用方法名来引入切入点表达式 */ //@Pointcut("execution(* com.text2.C1.*(..))") public void declareJointPointExpression(){} /** * 声明该方法是一个前置通知, * 在目标方法开始之前执行 * @param joinPoint */ //@Before("declareJointPointExpression()") public void beforeMethod(JoinPoint joinPoint){ //得到方法的名字 String name = joinPoint.getSignature().getName(); //得到方法的参数 List<Object> list = Arrays.asList(joinPoint.getArgs()); System.out.println("The beforeMethod :"+name+list); } /** * 声明该方法是一个后置通知,在连接点返回结果或者抛出异常的时候, * 下面的后置通知记录了方法的终止, * 在后置通知中还不能访问目标方法执行的结果 * @param joinPoint */ // @After("declareJointPointExpression()") public void afterMethod(JoinPoint joinPoint){ //得到方法的名字 String name = joinPoint.getSignature().getName(); //得到方法的参数 List<Object> list = Arrays.asList(joinPoint.getArgs()); System.out.println("The afterMethod :"+name+list); } /** * 返回通知,返回通知是可以访问到方法的返回值的 * @param joinPoint * @param result */ // @AfterReturning(value = "declareJointPointExpression()",returning = "result") public void afterReturning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); System.out.println("The afterReturning "+name+" ends with "+ result.toString()); } /** * 异常通知,在目标方法出现异常时会执行的代码, * 可以访问到异常对象;且可以指定在出现特定异常时在执行通知代码 * @param joinPoint * @param ex */ // @AfterThrowing(value = "declareJointPointExpression()",throwing = "ex") public void afterThrowing(JoinPoint joinPoint,Exception ex){ String name = joinPoint.getSignature().getName(); System.out.println("The afterThrowing "+name+" ends with "+ ex.toString()); } /** * 环绕通知:跟动态代理一样,需要携带ProceedingJoinPoint类型的参数 * 环绕通知类似于动态代理的全过程ProceedingJoinPoint类型的参数可以决定是否执行目标 4000 * 且必须有返回值,返回值为目标方法的返回值 * @param proceedingJoinPoint * @return */ //@Around(value = "declareJointPointExpression()") public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){ Object o = null; String methodName = proceedingJoinPoint.getSignature().getName(); //执行目标方法 try { //前置通知 System.out.println("The aroundMethod "+methodName+" begins with "+ Arrays.asList(proceedingJoinPoint.getArgs())); o = proceedingJoinPoint.proceed(); //返回通知 System.out.println("The aroundMethod "+ methodName +" ends with "+ o); } catch (Throwable throwable) { throwable.printStackTrace(); //异常通知 System.out.println("The aroundMethod "+ methodName +" occurs exception: "+ throwable); } //后置通知 System.out.println("The aroundMethod "+ methodName+" ends"); return o; } }
<?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: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/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"> <!--可以设置包含与不包含那些包下的与类,接口--> <context:component-scan base-package="com.text2"></context:component-scan> <!--使AspjectJ注解起作用:自动为匹配的对象生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--配置bean--> <bean id="c1" class="com.text2.C1Impl"></bean> <!--配置切面类的bean--> <bean id="c2" class="com.text2.C2"></bean> <!--配置AOP--> <aop:config> <!--配置切点表达式--> <aop:pointcut id="pointcut" expression="execution(* com.text2.C1.*(..))" /> <!--配置切面及通知的优先级--> <aop:aspect ref="c2" order="1"> <!--配置具体通知--> <aop:before method="beforeMethod" pointcut-ref="pointcut"/> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <!--环绕通知--> <!--<aop:around method="aroundMethod" pointcut-ref="pointcut" />--> </aop:aspect> </aop:config> </beans>
package com.text2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.*; public class TestC { @org.junit.Test public void b1() throws Exception { /*创建Spring的IOC容器*/ ApplicationContext ctx = new ClassPathXmlApplicationContext("text2.xml"); /*从IOC中获取bean的实例*/ C1 c1 = (C1) ctx.getBean("c1"); int result = c1.chu(10,2); System.out.println("值:"+result); } }
相关文章推荐
- spring学习笔记 -- day08 基于XML的Spring中的AOP
- spring框架的学习(四)——Spring的AOP的概述及AOP的操作(基于aspectj的xml方式)
- Spring学习总结7(AOP-基于XML)
- Spring 4.0 学习日记(9) ---XML配置实现AOP切面
- Spring学习(四)AOP_基于xml
- 基于xml配置的Spring AOP切面的几点总结,以及简单的例子
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- spring添加AOP切面-基于XML配置
- spring学习笔记(23)基于tx/aop配置切面增强事务
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- Spring学习--基于 XML 的配置声明切面
- spring学习总结(十):基于 XML 的配置AOP
- JavaWeb学习笔记-spring-17-AOP-基于schema配置切面
- Spring概念学习--面向切面AOP
- spring学习笔记(22)----基于配置文件的方式来配置AOP
- spring学习--面向切面编程AOP和AspectJ
- Spring学习4-面向切面(AOP)之schema配置方式
- 【学习】Spring 基于XML的配置及其原理