AOPのTraceInterceptor

來源:互聯網
上載者:User

1. TestAop.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC    "-//SEASAR//DTD S2Container 2.3//EN"    "http://www.seasar.org/dtd/components23.dtd">
<components>
    <component name="traceInterceptor"
               class="org.seasar.framework.aop.interceptors.TraceInterceptor"/>
    <component name="showLog"
               class="examples.di.main.ShowLog">
        <arg>"ShowLog"</arg> //建構函式注入
    </component>

    <component class="java.util.ArrayList">
        <aspect pointcut="size, hashCode">showLog</aspect>
    </component>
    <component class="java.util.Date">
        <arg>0</arg>
 <aspect pointcut="getTime, hashCode">showLog</aspect>
    </component>
</components>

 

2.ShowLog.java

需要繼承自MethodInterceptor或者AbstractInterceptor ,並實現invoke方法

package examples.di.main;

import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.interceptors.AbstractInterceptor;
import org.seasar.framework.aop.interceptors.TraceInterceptor;
import org.seasar.framework.log.Logger;

public class ShowLog extends AbstractInterceptor {
 private static final long serialVersionUID = 1L;
 private static final Logger logger = Logger
   .getLogger(TraceInterceptor.class);

 public ShowLog(String methodName) {
  System.out.println(methodName + " is begin");
 }

// public Object invoke(MethodInvocation invocation) throws Throwable {
//  System.out.println("Begin");
//  Object ret = invocation.proceed();
//  System.out.println("End");
//  return ret;
// }
 public Object invoke(MethodInvocation invocation) throws Throwable {
        long start = 0;
        long end = 0;
        StringBuffer buf = new StringBuffer(100);

        buf.append(getTargetClass(invocation).getName());
        buf.append("#");
        buf.append(invocation.getMethod().getName());
        buf.append("(");
        Object[] args = invocation.getArguments();
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; ++i) {
                buf.append(args[i]);
                buf.append(", ");
            }
            buf.setLength(buf.length() - 2);
        }
        buf.append(")");
        try {
            start = System.currentTimeMillis();
            Object ret = invocation.proceed();
            end = System.currentTimeMillis();
            buf.append(" : ");
            return ret;
        } catch (Throwable t) {
            buf.append(" Throwable:");
            buf.append(t);
            throw t;
        } finally {
            System.out.println(buf.toString() + (end - start));
        }
    }

}

 

3.TestAop.java

package examples.di.main;

import java.util.Date;
import java.util.List;

import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

public class TestAop {

    private static final String PATH =
        "TestAop.dicon";

    public static void main(String[] args) {

     S2Container container = S2ContainerFactory.create(PATH);
     container.init();
     List list = (List) container.getComponent(List.class);
     list.size();
     list.clear();
     list.hashCode();
     Date date = (Date) container.getComponent(Date.class);
     date.getTime();
     date.hashCode();
     date.toString();

    }
}

4.運行結果

log4j:WARN No appenders could be found for logger (org.seasar.framework.container.factory.S2ContainerFactory).
log4j:WARN Please initialize the log4j system properly.
ShowLog is begin
java.util.ArrayList#size() : 0
java.util.ArrayList#size() : 0
java.util.ArrayList#hashCode() : 0
java.util.Date#getTime() : 0
java.util.Date#getTime() : 0
java.util.Date#hashCode() : 0

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.