Spring Aspect Logging : Spring Aspect « Spring « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Spring » Spring Aspect 
28. 50. 2. Spring Aspect Logging

File: IBusinessLogic.java

public interface IBusinessLogic
{
   public void foo();
   
   public void bar() throws BusinessLogicException;
}

File: LoggingThrowsAdvice.java

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

public class LoggingThrowsAdvice implements ThrowsAdvice
{
   public void afterThrowing(Method method, Object[] args, Object target, Throwable subclass)
   {
      System.out.println("Logging that a " + subclass + "Exception was thrown.");
   }
}

File: MainApplication.java

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

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

      //Instantiate an object
      IBusinessLogic testObject = (IBusinessLogicctx.getBean("businesslogicbean");

      //Execute the public methods of the bean
      testObject.foo();
      
      try
      {
         testObject.bar();
      }
      catch(BusinessLogicException ble)
      {
         System.out.println("Caught BusinessLogicException");
      }
   }
}

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 targetthrows 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 targetthrows 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()");
     }
     
     public void bar() throws BusinessLogicException
     {
        System.out.println("Inside BusinessLogic.bar()");
        throw new BusinessLogicException();
     }
}

File: BusinessLogicException.java

public class BusinessLogicException extends Exception
{

}

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>tracingBeforeAdvisor</value>
        <value>tracingAfterAdvisor</value>
        <value>loggingThrowsAdvisor</value>
      </list>
    </property>
  </bean>

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

  <!-- Advisor pointcut definition for before advice -->
  <bean id="tracingBeforeAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theTracingBeforeAdvice"/>
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>
  
  <!-- Advisor pointcut definition for after advice -->
  <bean id="tracingAfterAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theTracingAfterAdvice"/>
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>
  
  <!-- Advisor pointcut definition for throws advice -->
  <bean id="loggingThrowsAdvisor"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theLoggingThrowsAdvice"/>
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--ADVICE-->
  <bean id="theTracingBeforeAdvice"
  class="TracingBeforeAdvice"/>
  <bean id="theTracingAfterAdvice"
  class="TracingAfterAdvice"/>
  <bean id="theLoggingThrowsAdvice"
  class="LoggingThrowsAdvice"/>
</beans>
  Download:  Spring-SpringAspectLogging.zip( 1,540 k)
28. 50. Spring Aspect
28. 50. 1. Spring Tracing Aspect
28. 50. 2. Spring Aspect Logging
28. 50. 3. AspectJ Expression Pointcut
28. 50. 4. AspectJ AutoProxy
28. 50. 5. Aspect Filter
28. 50. 6. Aspect Annotation Pointcut Around After
28. 50. 7. Aspect Annotation
28. 50. 8. AOP Annotation
28. 50. 9. Annotation Scope
28. 50. 10. Spring AOP Examples
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.