Spring4-aspect日志管理实例
来源:互联网 发布:飞鹰网络电视手机版g 编辑:程序博客网 时间:2024/06/06 02:36
Spring-常用Jar包
[http://pan.baidu.com/s/1dD1t92D]
aspect实例
所需jar包如上
bean配置文件aspect.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 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-4.0.xsd"><!-- 配置自动扫描 --><context:component-scan base-package="com.bean.aopiml.aspect"></context:component-scan><!-- 使aspjectj 起作用 :自动为匹配的类生成代理对象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
日志管理-切面类
package com.bean.aopiml.aspect;import java.util.Arrays;import java.util.List;import org.springframework.stereotype.Component;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;/* * 把这个类声明为一个切面:首先要放入到ioc容器中,然后在声明切面 * */@Aspect@Componentpublic class LoggingAspect { @Before("execution(public * com.bean.aopiml.aspect.ArithmeticInter.*(int, int))") public void beforeMethod(JoinPoint joinPoint){ String methodName =joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("Method - "+methodName+" begins with"+args); } /* * 在目标方法执行后(无论是否发生异常) 后置通知 * 不能访问目标方法执行结果 * */ @After("execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName =joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("Method - "+methodName+" end with"+args); }@AfterReturning(value="execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))",returning="result") public void afterReturning(JoinPoint joinPoint,Object result){ String methodName =joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("Method - "+methodName+" result with :"+result); }@AfterThrowing(value="execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))" ,throwing="ex")public void afterThrowing(JoinPoint joinPoint,Exception ex){ String methodName =joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("Method - "+methodName+" occurs exception :"+ ex);}/* * 环绕通知需要携带 ProceedingJoinPoint 类型参数 * 环绕通知类似于动态代理的全过程 ProceedingJoinPoint 类型的参数可以决定是否执行目标方法 * 环绕通知 必须有返回值 且返回值就是方法的返回值 * */@Around("execution(* com.bean.aopiml.aspect.ArithmeticInter.*(..))")public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){ String methodName =proceedingJoinPoint.getSignature().getName(); Object result=null; List<Object> args=Arrays.asList(proceedingJoinPoint.getArgs()); try { //前置通知 System.out.println("Method - "+methodName+" begins with"+args); //执行目标方法 result=proceedingJoinPoint.proceed(); //返回通知 System.out.println("Method - "+methodName+" result with :"+result); } catch (Throwable e) { // TODO Auto-generated catch block // 异常通知 System.out.println("Method - "+methodName+" occurs exception :"+ e); e.printStackTrace(); } //后置通知 System.out.println("Method - "+methodName+" end with"+args); return result;}}
实例其他普通接口与类
- ArithmeticInter
package com.bean.aopiml.aspect;public interface ArithmeticInter { public int add(int i,int j); public int sub(int i,int j); public double mul(int i,int j); public double div(int i,int j);}
- Arithmetic
package com.bean.aopiml.aspect;import org.springframework.stereotype.Component;@Componentpublic class Arithmetic implements ArithmeticInter{ public int add(int i,int j){ int result=i+j; return result; } public int sub(int i,int j){ int result=i-j; return result; } public double mul(int i ,int j){ double result=i*j; return result; } public double div(int i ,int j){ double result=i/(double)j; return result; }}
- JunitTest
package com.bean.aopiml.aspect;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class JunitTest { ArithmeticInter arithmetic; @Before public void setUp() throws Exception { ApplicationContext ctx=new ClassPathXmlApplicationContext("com/bean/aopiml/aspect/beans-aspect.xml"); arithmetic =ctx.getBean(ArithmeticInter.class); } @After public void tearDown() throws Exception { } @Test public void test() { arithmetic.add(3, 6); arithmetic.sub(3, 6); arithmetic.mul(3, 6); arithmetic.div(10, 0); }}
0 0
- Spring4-aspect日志管理实例
- spring4.0 aspect 失效
- spring Aspect 切面日志
- Spring Aspect简单实例
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式)
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+动态修改注解Log)
- logAnalyzer日志管理系统配置实例
- spring4.0 使用@Aspect 报错之缺少包
- SPRING4学习日志1
- spring4+hessian实例^_^
- Spring4--Hello World实例
- Spring4 MVC HelloWord实例
- SpringMVC+Spring4+hibernate4实例
- Spring4+WebSocket实例
- spring boot @Aspect 统一日志处理
- Log4j日志管理的简单实例
- Aspect
- linux下LTP工具说明
- Qt开发:最小化到托盘
- Session
- 软件流程图绘制方法
- UVA 11732 "strcmp()" Anyone? Trie树 + 树的左儿子右兄弟存储法
- Spring4-aspect日志管理实例
- python Django 1.7 中文入门 (官网) 02 django安装设置
- 【Jason's_ACM_解题报告】矩形嵌套(NYOJ16)
- 利用Fragment实现Tab选项效果总结
- 引用和指针的地址是相同的!
- 安卓倒计时按钮
- Ext JS4序列教程之一 :Data Grid数据表格控件
- JavaWeb_文件上传工具
- index.html 怎么导入一个top.html 和footer.html呢?