1, 使用代理增加日志, 也是基于最原始的办法

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class LoggingProxy {
/*
* 代理类, 基于接口
*/
//要被代理的对象, 目标对象
private Icalculator target;
//生成一个构造方法
public LoggingProxy(Icalculator target) {
super();
this.target = target;
}
//应用通知. 并产生对象
public Icalculator getProxy() {
Icalculator ica = null;
//应用通知
//获得类加载器: ClassLoader, 类加载器在getClass()方法里面
ClassLoader cl = target.getClass().getClassLoader(); //获得class中所有方法的数组, 数组的内容一定要是接口.class
Class[] cla= new Class[] {Icalculator.class};
//Class[] al = new Class[] {IJiSuanQi.class};//接口 //获得
InvocationHandler ih = new InvocationHandler() {
@Override
//调用invoke的时候就是实现一个切面编程
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//在这之前可以增加数据验证
Object obj = null;
System.out.println(method.getName()+" 开始调用");
try {
obj = method.invoke(target, args);
}
catch(Exception e) {
System.out.println("异常通知: "+method.getName());
}
System.out.println(method.getName()+" 结束调用");
return obj;
}
};
//产生代理对象, 引用了反射的jar包
ica = (Icalculator)Proxy.newProxyInstance(cl, cla, ih);
return ica;
}
}

2, 使用AOP框架

配置文件

    <!-- 前面定义的类class -->
<bean id="cal" class="com.hanqi.Calculator">
</bean> <!-- 切面类 -->
<bean id="la" class="com.hanqi.LoggingAspect">
</bean> <!-- 定义AOP -->
<aop:config>
<!-- 配置切点表达式, 被切入的方法 -->
<!-- expression写对象必须是个接口被切入方法的名字, 如果要写所有的方法就用*号表示 -->
<aop:pointcut expression="execution(* com.hanqi.Icalculator.*(int,int))" id="loggingpointcut"/> <!-- 配置切面和通知 -->
<aop:aspect ref="la">
<!-- 方法前通知 -->
<aop:before method="beforeMethod" pointcut-ref="loggingpointcut"/>
<aop:after method="afterMethod" pointcut="execution(* com.hanqi.Icalculator.cheng(int,int))"/>
<aop:after-throwing method="exceptionMethod" pointcut-ref="loggingpointcut" throwing="ex"/>
<aop:after-returning method="returnMethod" pointcut-ref="loggingpointcut" returning="obj"/>
</aop:aspect> </aop:config>

定义切面类

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.springframework.stereotype.Component; public class LoggingAspect {
//切面类, 前置通知
public void beforeMethod(JoinPoint jp) {
//获取方法名
String str = jp.getSignature().getName();
//返回一个参数列表
Object[] obj = jp.getArgs();
System.out.println("方法名 = "+str);
System.out.println(Arrays.asList(obj));
System.out.println("这里是方法前的通知");
}
public void afterMethod(JoinPoint jp) {
System.out.println("这里是方法后的通知");
}
//异常通知
public void exceptionMethod(JoinPoint jp,Exception ex) {
System.out.println("异常信息"+ex);
} //返回通知
public void returnMethod(JoinPoint jp,Object obj) {
System.out.println("返回通知的结果: "+obj);
}
}

3, 使用注解的方式(标红的部分是注解), 一定记得写上返回值和, expression表达式

import org.springframework.stereotype.Component;

@Component("cal")
public class Calculator implements Icalculator {
......
方法体
......
}
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.springframework.stereotype.Component; @Aspect
@Component
public class LoggingAspect {
//切面类, 前置通知
@Before(value = "execution(* com.hanqi.Calculator.*(..))")
public void beforeMethod(JoinPoint jp) {
//获取方法名
String str = jp.getSignature().getName();
//返回一个参数列表
Object[] obj = jp.getArgs();
System.out.println("方法名 = "+str);
System.out.println(Arrays.asList(obj));
System.out.println("这里是方法前的通知");
}
//后置通知
@After("execution(* com.hanqi.Calculator.*(..))")
public void afterMethod(JoinPoint jp) {
System.out.println("这里是方法后的通知");
}
//异常通知
@AfterThrowing(pointcut="execution(* com.hanqi.Calculator.*(..))", throwing="ex")
public void exceptionMethod(JoinPoint jp,Exception ex) {
System.out.println("异常信息"+ex);
} //返回通知
@AfterReturning(pointcut="execution(* com.hanqi.Calculator.*(..))", returning="obj")
public void returnMethod(JoinPoint jp,Object obj) {
System.out.println("返回通知的结果: "+obj);
}
}

注解的配置文件

<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.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.2.xsd"> <!-- 使用注解的方式 -->
<!-- 扫描器 -->
<context:component-scan base-package="com.hanqi"></context:component-scan>
<!-- 启用AOP注解 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

Spring/AOP框架, 以及使用注解的更多相关文章

  1. Spring AOP 框架

    引言 要掌握 Spring AOP 框架,需要弄明白 AOP 的概念. AOP 概念 AOP(Aspect Oriented Programming的缩写,翻译为面向方面或面向切面编程),通过预编译方 ...

  2. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  3. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  4. spring AOP (使用AspectJ的注解方式 的aop实现) (6)

    目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...

  5. Spring AOP框架 AspectJ

    1 AspectJ简介 v  AspectJ是一个基于Java语言的AOP框架 v  Spring2.0以后新增了对AspectJ切点表达式支持 v  @AspectJ 是AspectJ1.5新增功能 ...

  6. Spring AOP(5)-- 注解

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  7. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  8. 【Spring AOP】Spring AOP之如何通过注解的方式实现各种通知类型的AOP操作进阶篇(3)

    一.切入点表达式的各种类型 切入点表达式的作用:限制连接点的匹配(满足时对应的aspect方法会被执行) 1)execution:用于匹配方法执行连接点.Spring AOP用户可能最经常使用exec ...

  9. 循序渐进之Spring AOP(6) - 使用@Aspect注解

    前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...

随机推荐

  1. 我的JdbcUtils类

    这是目录结构: 其中后面三个类都是第一个类的子类,第一个类是父类. JdbcUtils: package com.smt.db; import java.io.IOException; import ...

  2. 使用alter database datafile 'XXX' offline drop 是否能够恢复(非归档模式下)

    今天在群里面听到一位网友在说使用了alter database datafile 'XXX' offline drop命令是否能够恢复数据,在非归档模式下,下面是用一个实验来验证一下 ######## ...

  3. mysql 笔记分享

    mysql LPAD 和RPAD不足位数补齐填充函数总结一下mysql数据库的一些特征MySQL WHERE 语句优化之我见mysql limit 实例详解mysql 如何实现多表联合更新MySQL ...

  4. 并发基础(三) java线程优先级

      在不同的JVM中(JVM也算是一个操作系统),有着不同的CPU调度算法,对于大部分的JVM来说,优先级也是调度算法中的一个参数.所以,线程优先级在一定程度上,对线程的调度执行顺序有所影响,但不能用 ...

  5. Python之函数——进阶篇

    嵌套函数 ---函数内部可以再次定义函数 ---函数若想执行,必须被调用 注意,下例中,执行结果为什么? age = 19 def func1(): print(age) def func2(): p ...

  6. java技术-重点方向

    多线程 锁 事务 缓存 hashmap 并发编程

  7. C++官方文档-this

    #include <iostream> using namespace std; class Dummy { public: int x; Dummy() : x() { } ; Dumm ...

  8. JS 操作 file标签只上传照片

    在当前高版本浏览器里 在标签里加这个属性就够用了 accept="image/*" $('input[type="file"]').live('change', ...

  9. 高性能JSON框架之FastJson的简单使用

    1.前言 1.1.FastJson的介绍: JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的 ...

  10. Java Future 和 FutureTask 源码Demo

    Future 是一个接口,看源码有Future 和 FutreTask 使用Demo package java.util.concurrent; /** * A <tt>Future< ...