Spring-注解的方式配置AOP-案例
来源:互联网 发布:张海山锐线体简 mac 编辑:程序博客网 时间:2024/04/26 19:32
需求:分别在AtithmeticCalculator.java实现了该接口的类的加减乘除的方法执行时打印方法执行情况,并在方法执行前判断所有参数是否都是int,用aop实现。
接口实现类:AtithmeticCalculatorImpl.java
/* * 文件名:AtithmeticCalculatorImpl.java * 版权:Copyright by www.huawei.com * 描述: * 修改人:Cuigaochong * 修改时间:2015-11-2 * 跟踪单号: * 修改单号: * 修改内容: */package com.cgc.spring.aop.impl;import org.springframework.stereotype.Component;/** * <一句话功能简述> <功能详细描述> * * @author 姓名 工号 * @version [版本号, 2015-11-2] * @see [相关类/方法] * @since [产品/模块版本] */@Componentpublic class AtithmeticCalculatorImpl implements AtithmeticCalculator{ /** * 重载方法 * * @param i * @param j * @return */ @Override public int add(int i, int j) { int result = i + j; return result; } /** * 重载方法 * * @param i * @param j * @return */ @Override public int sub(int i, int j) { int result = i - j; return result; } /** * 重载方法 * * @param i * @param j * @return */ @Override public int mul(int i, int j) { int result = i * j; return result; } /** * 重载方法 * * @param i * @param j * @return */ @Override public int div(int i, int j) { int result = 0; try { //前置通知 result = i / j; //返回通知 } catch (Exception e) { e.printStackTrace(); //异常通知,可以访问到出现的异常 } //后置通知,因为方法可能会出现异常,所以访问不到方法的返回值 return result; } }第一个切面类:VlidationAspect.java实现判断请求的参数是不是int
/* * 文件名:VlidationAspect.java * 版权:Copyright by www.huawei.com * 描述: * 修改人:Cuigaochong * 修改时间:2015-11-2 * 跟踪单号: * 修改单号: * 修改内容: */package com.cgc.spring.aop.impl;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;/** * <一句话功能简述> * <功能详细描述> * * @author 姓名 工号 * @version [版本号, 2015-11-2] * @see [相关类/方法] * @since [产品/模块版本] *///@Order(1)设定切面的优先级 值越小 优先级越高@Order(1)@Aspect@Componentpublic class VlidationAspect{ //不同的包下要加包名 com.cgc.spring.aop.impl.LoggingAspect.declareJointPointExpression() @Before("LoggingAspect.declareJointPointExpression()") public void validateArrgs(JoinPoint joinPoint) { System.out.println("-->validate"+Arrays.asList(joinPoint.getArgs())); //可以在此处实现请求的参数是不是int类型,从joinPoint中可以获取请求的参数 }}第二个切面类:在不同方法执行前,打印不同方法的执行日志:LoggingAspect.java
/* * 文件名:LoggingAspect.java * 版权:Copyright by www.huawei.com * 描述: * 修改人:Cuigaochong * 修改时间:2015-11-2 * 跟踪单号: * 修改单号: * 修改内容: */package com.cgc.spring.aop.impl;import java.util.Arrays;import java.util.List;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;/** * <一句话功能简述> * <功能详细描述> * * @author 姓名 工号 * @version [版本号, 2015-11-2] * @see [相关类/方法] * @since [产品/模块版本] *///把一个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面@Order(2)@Aspect@Componentpublic class LoggingAspect{ //定义一个方法,用于声明切入点表达式,一般的 该方法中不需要再添加其他代码 @Pointcut("execution(public int com.cgc.spring.aop.impl.AtithmeticCalculatorImpl.*(..) )") public void declareJointPointExpression() { } //声明该方法是一个前置通知,在目标方法开始之前执行.第一个*表示任意修饰符 任意返回值,后边的*表示任意方法名 //@Before("execution(* com.cgc.spring.aop.impl.AtithmeticCalculatorImpl.*(int, int) )") @Before("declareJointPointExpression()") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("this is method "+methodName +" begin with "+ args); } //后置通知:在目标方法执行后(无论是否发生异常),执行的通知 //在后置通知中不能访问在目标方法中返回的执行结果 @After("execution(public int com.cgc.spring.aop.impl.AtithmeticCalculatorImpl.*(int, int) )") public void afterMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); System.out.println("this is method "+methodName +" end with "); } //在方法正常结束后要执行的代码 //返回通知是可以访问到方法的返回值的 @AfterReturning(value="declareJointPointExpression()",returning="result") public void afterReturning(JoinPoint joinPoint,Object result) { String methodName = joinPoint.getSignature().getName(); System.out.println("this is method "+methodName +" after Returning " + result); } //在方法出现异常时,会执行的代码,而且可以访问到异常对象,且可以指定在出现 特定异常时在执行通知代码 Exception ex,NullPointerException ex @AfterThrowing(value="declareJointPointExpression())",throwing="ex") public void afterThrowing(JoinPoint joinPoint , Exception ex) { String methodName = joinPoint.getSignature().getName(); System.out.println("this is method "+methodName +" after Returning exception " + ex); }}配置文件: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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.cgc.spring.aop.impl"></context:component-scan><!-- 使Aspject 注解其作用:自动为匹配的类生成代理对象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>通过main调用方法:
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AtithmeticCalculator cal = ac.getBean(AtithmeticCalculator.class); System.out.println(cal.getClass().getName()); int result = cal.add(3, 4); System.out.println(result); int result00 = cal.div(10, 2); System.out.println(result00); }
1 0
- Spring-注解的方式配置AOP-案例
- spring中aop的注解配置方式
- Spring Aop开发基于AspectJ注解方式的案例
- Spring 注解方式配置aop:
- 注解方式配置spring-aop
- spring aop xml方式的配置案例 以及 annotation方式的配置aop
- Spring配置入门案例(注解方式 )
- spring aop 注解方式
- 【Spring进阶】spring对AOP的支持-注解方式和配置方式
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- Spring使用注解的方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- iOS CoreData之单表操作(增删改查)
- Redis概念
- CGAffineTransform对View的缩放、平移和旋转
- Fragment基础点滴学习---Fragment的生命周期
- 杭电 3792 Twin Prime Conjecture
- Spring-注解的方式配置AOP-案例
- 使用WebView 进行网络开发
- UIImageView
- soj 3329: Maximum Submatrix II(单调栈)
- lua学习01——基础
- 首次加载和回发
- iOS新闻客户端教程5-Home主页
- ftp推送
- 图片选择器