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. VBA注释临时

    Sub shishi() '按ABCDE为多选题定义答案; 'A.沙利度胺 B.异烟肼 C.利福平 'd.氯法齐明 E.氨苯砜 '46.各型麻风病的首选药物为(D) 'A.沙利度胺 B.异烟肼 C.利 ...

  2. java web程序 登陆验证页面 4个页面人性化设置

    到这里,快期末考试了,老师不讲课,我心里有苦不想说,也许没有考虑到老师的感受,让老师难堪了 但是我的行为已不再是我可以做的了.不可能了,我只是职业性的机械的做事了. 思路: 1.第一个是form表单, ...

  3. select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;

    子查询必须加一个别名才能执行!!

  4. vmware12共享windows的文件给虚拟的linux

    1:首先我的vmware的版本是12的 点击vmware的虚拟机---------------------->设置------------------------>选项---------- ...

  5. SQL的三种连接方式内连接、左连接、外连接

    1.内连接 select * from table_a x inner join table_b y on x.a_id = y.b_id 返回两个表关键字x.a_id = y.b_id的交集数据集 ...

  6. tornado系列文章

    http://www.nowamagic.net/academy/detail/13321030

  7. [转]Tornado get/post请求异步处理框架分析

    转自: http://m.blog.csdn.net/blog/joeyon/41956027 首先说下环境,python2.7,Tornado3.0 然后本文讨论的不是Tornado底层如何用epo ...

  8. Spectrum Scale

    高端存储:2016年为止,最新产品为DS8884.DS8886和DS8888. 闪存系统:2016年为止,最新产品,以FlashSystem 900为硬件基础,包装了FlashSystem V9000 ...

  9. [Python] 拉格朗日插值

    #-*— coding:utf-8 -*- #Program 0.3 Lagrange Interpolation import matplotlib.pyplot as plt import num ...

  10. 怎么理解Linux软中断?

    1.什么是中断 中断是系统用来响应硬件设备请求的一种机制,它会打断进程的正常调度和执行,然后调用内核中的中断处理程序来响应设备的请求. 2.为什么要有中断呢? "举个生活中的例子" ...