面向方面编程(aop)是一种新的规范,允许你达到以前面对对象方法无法达到的组织和分层你的应用程序的方法. 方面允许你很明显的把功能性的东西粘合起来,所以你就可以很方便的为程序设计很多的层。aop可以拦截所有java程序中的事件触发。
什么是aop?
一个方面(aspect)就是一个共有的特性,具有此代表性的有:横向分离的方法,类,对象层次或者实体对象模型。它们看起来应该是组合在一起的,但是在aop里面你不用像以前面对对象(oo)那样来组织它们了。
在传统java中要加入计算时间的代码到你的应用中,你必须按以下方式:
public class bankaccountdao
{
public void withdraw(double amount)
{
long starttime = system.currenttimemillis();
try
{
// actual method body...
}
finally
{
long endtime = system.currenttimemillis() - starttime;
system.out.println("withdraw took: " + endtime);
}
}
}
01. public class metrics implements org.jboss.aop.interceptor
02. {
03. public object invoke(invocation invocation) throws throwable
04. {
05. long starttime = system.currenttimemillis();
06. try
07. {
08. return invocation.invokenext();
09. }
10. finally
11. {
12. long endtime = system.currenttimemillis() - starttime;
13. java.lang.reflect.method m = ((methodinvocation)invocation).method;
14. system.out.println("method " + m.tostring() + " time: " + endtime + "ms");
15. }
16. }
17. }
1. <bind pointcut="public void com.mc.bankaccountdao->withdraw(double amount)">
2. <interceptor class="com.mc.metrics"/>
3. </bind >
4. <bind pointcut="* com.mc.billing.*->*(..)">
5. <interceptor class="com.mc.metrics"/>
6. </bind >
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器