Spring的AOP编程XML配置
来源:互联网 发布:手机淘宝客服图片 编辑:程序博客网 时间:2024/05/21 18:31
1、applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan><!-- 配置自动为匹配 aspectJ 注解的 Java 类生成代理对象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
2、applicationContext-xml.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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!-- 配置 bean --><bean id="arithmeticCalculator" class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean><!-- 配置切面的 bean. --><bean id="loggingAspect"class="com.atguigu.spring.aop.xml.LoggingAspect"></bean><bean id="vlidationAspect"class="com.atguigu.spring.aop.xml.VlidationAspect"></bean><!-- 配置 AOP --><aop:config><!-- 配置切点表达式 --><aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" id="pointcut"/><!-- 配置切面及通知 --><aop:aspect ref="loggingAspect" order="2"><aop:before method="beforeMethod" pointcut-ref="pointcut"/><aop:after method="afterMethod" pointcut-ref="pointcut"/><aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/><aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/><!-- <aop:around method="aroundMethod" pointcut-ref="pointcut"/>--></aop:aspect><aop:aspect ref="vlidationAspect" order="1"><aop:before method="validateArgs" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>
Java
1、com.atguigu.spring.aop包
1)ArithmeticCalculator.java
package com.atguigu.spring.aop;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}
2)ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop;import org.springframework.stereotype.Component;@Component("arithmeticCalculator")public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}}
3)LoggingAspect.java
package com.atguigu.spring.aop;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * 可以使用 @Order 注解指定切面的优先级, 值越小优先级越高 */@Order(2)@Aspect@Componentpublic class LoggingAspect {/** * 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码. * 使用 @Pointcut 来声明切入点表达式. * 后面的其他通知直接使用方法名来引用当前的切入点表达式. */@Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public void declareJointPointExpression(){}/** * 在 com.atguigu.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码 */@Before("declareJointPointExpression()")public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();Object [] args = joinPoint.getArgs();System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));}/** * 在方法执行之后执行的代码. 无论该方法是否出现异常 */@After("declareJointPointExpression()")public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends");}/** * 在方法法正常结束受执行的代码 * 返回通知是可以访问到方法的返回值的! */@AfterReturning(value="declareJointPointExpression()",returning="result")public void afterReturning(JoinPoint joinPoint, Object result){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends with " + result);}/** * 在目标方法出现异常时会执行的代码. * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码 */@AfterThrowing(value="declareJointPointExpression()",throwing="e")public void afterThrowing(JoinPoint joinPoint, Exception e){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " occurs excetion:" + e);}/** * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法. * 且环绕通知必须有返回值, 返回值即为目标方法的返回值 *//*@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;String methodName = pjd.getSignature().getName();try {//前置通知System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));//执行目标方法result = pjd.proceed();//返回通知System.out.println("The method " + methodName + " ends with " + result);} catch (Throwable e) {//异常通知System.out.println("The method " + methodName + " occurs exception:" + e);throw new RuntimeException(e);}//后置通知System.out.println("The method " + methodName + " ends");return result;}*/}
4)Main.java
package com.atguigu.spring.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");System.out.println(arithmeticCalculator.getClass().getName());int result = arithmeticCalculator.add(1, 2);System.out.println("result:" + result);result = arithmeticCalculator.div(1000, 10);System.out.println("result:" + result);}}
5)VlidationAspect.java
package com.atguigu.spring.aop;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(1)@Aspect@Componentpublic class VlidationAspect {@Before("com.atguigu.spring.aop.LoggingAspect.declareJointPointExpression()")public void validateArgs(JoinPoint joinPoint){System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));}}
2、com.atguigu.spring.aop.xml包
1)ArithmeticCalculator.java
package com.atguigu.spring.aop.xml;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}
2)ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop.xml;public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}}
3)LoggingAspect.java
package com.atguigu.spring.aop.xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;public class LoggingAspect {public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();Object [] args = joinPoint.getArgs();System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));}public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends");}public void afterReturning(JoinPoint joinPoint, Object result){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends with " + result);}public void afterThrowing(JoinPoint joinPoint, Exception e){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " occurs excetion:" + e);}@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;String methodName = pjd.getSignature().getName();try {//前置通知System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));//执行目标方法result = pjd.proceed();//返回通知System.out.println("The method " + methodName + " ends with " + result);} catch (Throwable e) {//异常通知System.out.println("The method " + methodName + " occurs exception:" + e);throw new RuntimeException(e);}//后置通知System.out.println("The method " + methodName + " ends");return result;}}
4)VlidationAspect.java
package com.atguigu.spring.aop.xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;public class VlidationAspect {public void validateArgs(JoinPoint joinPoint){System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));}}
5)Main.java
package com.atguigu.spring.aop.xml;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");System.out.println(arithmeticCalculator.getClass().getName());int result = arithmeticCalculator.add(1, 2);System.out.println("result:" + result);result = arithmeticCalculator.div(1000, 0);System.out.println("result:" + result);}}
0 0
- Spring的AOP编程XML配置
- Spring、XML配置AOP
- spring aop xml配置
- Spring的AOP使用xml配置
- 基于XML的spring AOP配置
- Spring的AOP配置(基于xml)
- Spring -- 基于XML的AOP通知配置
- 基于XML的Spring AOP配置
- 配置xml方式实现Spring的aop
- Spring的AOP使用xml配置
- spring aop的xml配置详解
- 十五Spring Aop的xml配置方式
- AOP切面编程applicationContext.xml的配置
- spring(五)---aspectj aop编程(xml配置)
- Spring AOP配置(Annotation;Xml)
- Spring-XML配置AOP-案例
- Spring AOP之XML配置
- 使用XML配置Spring AOP
- 碰撞检测
- java8 lambda表达式-函数接口
- shell浅谈之进程处理
- Unity基础知识总结
- hadoop中使用MapReduce编程实例(转)
- Spring的AOP编程XML配置
- c++原来结构体这样初始化也行,一直不知道呢?
- 华为机试—约瑟夫环
- shell中各种括号的作用()、(())、[]、[[]]、{}
- C# 中值类型和引用类型的区别
- HDU 5155 Harry And Magic Box(组合+容斥)
- Jsp连接MSSQL数据库
- shell浅谈之八I/O重定向
- 网络爬虫技术浅析