Implements MethodInterceptor To Create Profiling Advice : MethodInterceptor « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="afterBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
      <bean class="MtBean">
        <property name="firstName" value="AAA"/>
      </bean>
    </property>
    <property name="interceptorNames">
      <list>
        <idref bean="profilingAdvice"/>
      </list>
    </property>
    <property name="proxyTargetClass" value="true"/>
  </bean>
  <bean id="profilingAdvice" class="SimpleProfilingAroundAdvice"/>
</beans>

File: Main.java

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StopWatch;

public class Main {

  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    MtBean testBean = (MtBean) beanFactory
        .getBean("afterBean");
    testBean.showValues();
  }
}

class MtBean {
  private String firstName;

  public String getFirstName() {
    return this.firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public void showValues() {
    System.out.println("First name: " + this.firstName);
  }
}

class SimpleProfilingAroundAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    try {
      return invocation.proceed();
    } finally {
      stopWatch.stop();
      System.out.println(stopWatch.prettyPrint());
    }
  }
}
  Download:  Spring-ImplementsMethodInterceptorToCreateProfilingAdvice.zip( 2,895 k)








28.55.MethodInterceptor
28.55.1.Create A Benchmark with MethodInterceptor
28.55.2.Check Invoked Method In MethodInterceptor
28.55.3.Implements MethodInterceptor To Create Profiling Advice
28.55.4.Implements MethodInterceptor