本篇文章为大家展示了怎么在不使用spring框架中使用aop的功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,Spring框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。
这里需要注意的是Spring AOP目前仅仅支持方法级别的切面,成员的interception并没有实现。另外,spring aop仅仅是集成框架,并没有参与aop的具体开发。
如果想利用aop的更多功能,或者在不使用spring的框架中使用aop的功能,该怎么办呢?
spring aop集成了AspectJ(可以和java编程语言无缝结合的一个面向切面编程的可扩展框架)
Eclipse Marketplace安装插件AJDT
创建Aspect工程
创建AspectJ测试类
创建一个切面Aspect文件
.aj文件
运行HelloAspectJDemo的java程序,结果为:
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("张三");
}
}
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("张三");
}
}
上述内容就是怎么在不使用spring框架中使用aop的功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。