Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,Spring框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。
spring提供了两种方式的AOP使用
使用xml配置方式

使用注解方式

这里需要注意的是Spring AOP目前仅仅支持方法级别的切面,成员的interception并没有实现。另外,spring aop仅仅是集成框架,并没有参与aop的具体开发。
如果想利用aop的更多功能,或者在不使用spring的框架中使用aop的功能,该怎么办呢?
AspectJ简介

spring aop集成了AspectJ(可以和java编程语言无缝结合的一个面向切面编程的可扩展框架)
AspectJ的使用实例
Eclipse Marketplace安装插件AJDT

创建Aspect工程

创建AspectJ测试类

创建一个切面Aspect文件

.aj文件

运行HelloAspectJDemo的java程序,结果为:

不使用spring的aop功能实现日志输出
第一种
- public class TimeBook {undefined
- ?private Logger logger = Logger.getLogger(this.getClass().getName());
- ?//审核数据的相关程序
- ?public void doAuditing(String name){undefined
- ? logger.log(Level.INFO, name + "开始审核数据...");
- ? System.out.println("审核程序");
- ? logger.log(Level.INFO, name + "审核数据结束...");
- ?}
- }
- //TestHelloWorld.java
- package com.gc.test;
- import com.gc.action.TimeBook;
- public class TestHelloWorld {undefined
- ?public static void main(String[] args){undefined
- ? TimeBook timeBook = new TimeBook();
- ? timeBook.doAuditing("张三");
- ?}
- }
第二种:通过面向接口编程实现日志输出
- public class TimeBook implements TimeBookInterface {undefined
- ?//审核数据的相关程序
- ?public void doAuditing(String name){undefined
- ? System.out.println("审核程序");
- ?}
- }
- //TimeBookProxy.java
- package com.gc.action;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import com.gc.impl.TimeBookInterface;
- public class TimeBookProxy {undefined
- ?private Logger logger = Logger.getLogger(this.getClass().getName());
- ?private TimeBookInterface timeBookInterface;
- ?//在该类中针对前面的接口TimeBookInterface编程,而不是针对具体的类
- ?public TimeBookProxy(TimeBookInterface timeBookInterface){undefined
- ? this.timeBookInterface = timeBookInterface;
- ?}
- ?//实际业务处理
- ?public void doAuditing(String name){undefined
- ? logger.log(Level.INFO,"开始审核数据 "+name);
- ? timeBookInterface.doAuditing(name);
- ? logger.log(Level.INFO,"审核数据结束 "+name);
- ?}
- }
- public class TestHelloWorld {undefined
- ?public static void main(String[] args){undefined
- ? TimeBookProxy timeBookProxy = new TimeBookProxy(new TimeBook());
- ? timeBookProxy.doAuditing("张三");
- ?}
- }
第三种:使用java的代理机制进行日志输出
- public class LogProxy implements InvocationHandler{undefined
- ?private Logger logger = Logger.getLogger(this.getClass().getName());
- ?private Object delegate;
- ?//绑定代理对象
- ?public Object bind(Object delegate){undefined
- ? this.delegate = delegate;
- ? return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
- ? ? delegate.getClass().getInterfaces(),this);
- ?}
- ?//针对接口编程
- ?public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {undefined
- ? Object result = null;
- ? try{undefined
- ? ?//在方法调用前后进行日志输出
- ? ?logger.log(Level.INFO,args[0]+" 开始审核数据...");
- ? ?result = method.invoke(delegate, args);
- ? ?logger.log(Level.INFO,args[0]+" 审核数据结束...");
- ? }catch(Exception e){undefined
- ? ?logger.log(Level.INFO,e.toString());
- ? }
- ? return result;
- ?}
- }
- //TimeBookInterface.java
- package com.gc.impl;
- //针对接口编程
- public interface TimeBookInterface {undefined
- ?public void doAuditing(String name);
- }
- //TimeBook.java
- public class TimeBook implements TimeBookInterface {undefined
- ?//审核数据的相关程序
- ?public void doAuditing(String name){undefined
- ? System.out.println("审核程序");
- ?}
- }
- //TestHelloWorld.java
- public class TestHelloWorld {undefined
- ?public static void main(String[] args){undefined
- ? //实现了对日志类的重用
- ? LogProxy logProxy = new LogProxy();
- ? TimeBookInterface timeBookProxy = (TimeBookInterface)logProxy.bind(new TimeBook());
- ? timeBookProxy.doAuditing("张三");
- ?}
- }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。