Spring Aspect Logging : Log « Spring « Java






Spring Aspect Logging

File: IBusinessLogic.java publicinterface IBusinessLogic { publicvoid foo(); publicvoid bar() throws BusinessLogicException; } File: LoggingThrowsAdvice.java import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; publicclass LoggingThrowsAdvice implements ThrowsAdvice { publicvoid 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; publicclass MainApplication { publicstaticvoid main(String [] args) { // Read the configuration file  ApplicationContext ctx = new FileSystemXmlApplicationContext("build/springconfig.xml"); //Instantiate an object  IBusinessLogic testObject = (IBusinessLogic) ctx.getBean("businesslogicbean"); //Execute the public methods of the bean  testObject.foo(); try { testObject.bar(); } catch(BusinessLogicException ble) { System.out.println("Caught BusinessLogicException"); } } } 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 forthrows 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> File: TracingAfterAdvice.java import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; publicclass TracingAfterAdvice implements AfterReturningAdvice { publicvoid 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; publicclass TracingBeforeAdvice implements MethodBeforeAdvice { publicvoid before(Method m, Object[] args, Object target) throws Throwable { System.out.println("Hello world! (by " + this.getClass().getName() + ")"); } } File: BusinessLogic.java publicclass BusinessLogic implements IBusinessLogic { publicvoid foo() { System.out.println("Inside BusinessLogic.foo()"); } publicvoid bar() throws BusinessLogicException { System.out.println("Inside BusinessLogic.bar()"); thrownew BusinessLogicException(); } } File: BusinessLogicException.java publicclass BusinessLogicException extends Exception { } 








Spring-SpringAspectLogging.zip( 1,540 k)

Related examples in the same category

close