Spring Tracing Aspect : AOP « Spring « Java






Spring Tracing Aspect

       
File: IBusinessLogic.java


public interface IBusinessLogic
{
   public void foo();
}


File: MainApplication.java


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApplication
{
   public static void main(String [] args)
   {
      ApplicationContext ctx = new FileSystemXmlApplicationContext("build/springconfig.xml");

      IBusinessLogic testObject = (IBusinessLogic) ctx.getBean("businesslogicbean");

      testObject.foo();
   }
}


File: springconfig.xml

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

<beans>

  <!--CONFIG-->
  <bean id="businesslogicbean"
  class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>IBusinessLogic</value>
    </property>
    <property name="target">
      <ref local="beanTarget"/>
    </property>
    <property name="interceptorNames">
      <list>
        <value>theTracingBeforeAdvisor</value>
        <value>theTracingAfterAdvisor</value>
      </list>
    </property>
  </bean>

  <!--CLASS-->
  <bean id="beanTarget"
  class="BusinessLogic"/>

  <!--ADVISOR-->
  <!--Note: An advisor assembles pointcuts and advice-->
  <bean id="theTracingBeforeAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theTracingBeforeAdvice"/>
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>
  
  <!--ADVISOR-->
  <!--Note: An advisor assembles pointcuts and advice-->
  <bean id="theTracingAfterAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theTracingAfterAdvice"/>
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--ADVICE-->
  <bean id="theTracingBeforeAdvice"
  class="TracingBeforeAdvice"/>
  <bean id="theTracingAfterAdvice"
  class="TracingAfterAdvice"/>
</beans>


File: TracingAfterAdvice.java

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class TracingAfterAdvice implements AfterReturningAdvice
{
   public void afterReturning(Object object, Method m, Object[] args, Object target) throws Throwable
   {
       System.out.println("Hello world! (by " + this.getClass().getName() + ")");
   }
}


File: TracingBeforeAdvice.java

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class TracingBeforeAdvice implements MethodBeforeAdvice
{
   public void before(Method m, Object[] args, Object target) throws Throwable
   {
      System.out.println("Hello world! (by " + this.getClass().getName() + ")");
   }
}


File: BusinessLogic.java


public class BusinessLogic implements IBusinessLogic
{
     public void foo() 
     {
       System.out.println("Inside BusinessLogic.foo()");
     }
}




           
       








Spring-SpringTracingAspect.zip( 1,538 k)

Related examples in the same category

1.Method Lookup
2.Method Before Advice
3.Matcher For Getter And Setter
4.Spring AOP Examples
5.Jdk Regexp Method Pointcut
6.Customizable TraceInterceptor
7.Concurrency Throttle Interceptor
8.ComposablePointcut Union
9.ComposablePointcut Intersection
10.AspectJ Expression Pointcut
11.AspectJ AutoProxy
12.Aspect Filter
13.Aspect Annotation Pointcut AroundAfter
14.Aspect Annotation
15.AOP Annotation
16.Annotation Scope
17.Annotation Component
18.Annotated Autowiring