Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: Formal Unbound in Pointcut

  1. #11

    Default The Advice

    Yeah, your're right. Guess it's kind of hard without seeing the advice (hides red face):

    Code:
    package com.uprr.aop;
    
    import java.lang.reflect.Method;
    
    import org.apache.log4j.Logger;
    import org.springframework.aop.ThrowsAdvice;
    
    import com.uprr.util.ApiLogger;
    
    //-----------------------------------------------------------------------
    /**
     * Invoked when an advised class throws a {@link Throwable}. The main purpose of this advice is
     * to ensure that all errors are recorded in the log.
     */
    public class ThrowAdvice implements ThrowsAdvice {
    
    //-----------------------------------------------------------------------
    /**
     * Reacts to an exception being thrown. Currently, this method just logs the exception.
     * @param method The throwing method.
     * @param args Arguments to the throwing method.
     * @param thrower The throwing object.
     * @param error The item thrown.
     * @throws RuntimeException if the API logger has not been configured.
     */
    public void afterThrowing(final Method method, final Object[] args, final Object thrower, 
    	final Throwable error) {
    	final Logger logger = Logger.getLogger(ApiLogger.NAME);
    	if (logger == null) {
    		String message = ApiLogger.NOT_FOUND;
    		message += " Error that could not be recorded: " + error;
    		throw new RuntimeException(message);
    	}
    	logger.error("An exception was thrown by " + thrower + '.' + method + '(' + args + ')');
    	logger.error("Details: " + error);
    }
    //-----------------------------------------------------------------------
    }

  2. #12
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The problem is you are mixing different kinds of aop. You shouldn't define a method to execute neither the throws clause in your xml. It will automatically detect that it is a Afterthrows advice.

    If you want to stick with your way. Remove the ThrowsAdvice interface and implement your method with a JoinPoint and an Exception.

    Code:
    public class ThrowAdvice {
    
    //-----------------------------------------------------------------------
    /**
     * Reacts to an exception being thrown. Currently, this method just logs the exception.
     * @param method The throwing method.
     * @param args Arguments to the throwing method.
     * @param thrower The throwing object.
     * @param error The item thrown.
     * @throws RuntimeException if the API logger has not been configured.
     */
    public void afterThrowing(JoinPoint jp, final Exception error) {
    	final Logger logger = Logger.getLogger(ApiLogger.NAME);
    	if (logger == null) {
    		String message = ApiLogger.NOT_FOUND;
    		message += " Error that could not be recorded: " + error;
    		throw new RuntimeException(message);
    	}
    	logger.error("An exception was thrown by " + jp.getSignature().getShortString());
    	logger.error("Details: " + error);
    }
    //-----------------------------------------------------------------------
    }
    Also I'm not sure if you want to throw a new RuntimeException if your logger cannot be found.... At least include your root exception if you rethrow an exception.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #13

    Default XML Setup

    Thanks for the quick analysis. Please be patient with me, as this is only my second day working with AOP & I've had no formal training.

    What is meant by "mixing different kinds of aop"?

  4. #14
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You are trying to mix AspectJ style AOP with the AOPAlliance style of AOP (that is where the ThrowsAdvice interface comes from).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #15

    Default How do Do XML?

    Well, maybe I should just ask it this way: How do I set up the XML file so the throws advice is executed when anything is thrown?

    I've tried numerous different ways of doing this in the XML config file, none of which works. I can't find a working example of this in either the Spring reference manual or in _Spring in Action_. Surely this must be a common task, but I can't seem to find it covered anywhere.

  6. #16
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I gave you the code and everything you need, what do you want more?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #17

    Default Can We Avoid JointPoint

    We'd rather not write the advice code if possible. I assume this would introduce an AspectJ dependency, which we'd like to avoid if possible, to keep the project as simple as we can.

    Can't we implement throws advice with ordinary Spring & not AspectJ? Can't we just change the XML somehow & not the advice code? We've been trying to follow the examples in Chapter 6 of the Spring reference manual, but so far it's been an exercise in frustration.

    Pardon my rant, but we're feeling very frustrated that a concept which seems so simple seems to be frighteningly complex.

  8. #18
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You already have a dependency on AspectJ (when using the aop:confgi stuff you already introduced that dependency). Next to that you use AspectJ pointcut expressions but don't want to use the rest.

    Code:
    <aop:config>
      <aop:pointcut id="allMethods" expression="execution(* *(..))" />
      <aop:advisor pointcut-ref="allMethods" advice-ref="throwsAdvice" />
    </aop:config>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #19

    Default New Error - CGLIB

    Ok, I changed the config file to this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       "
       default-autowire="byName"
    >
       <bean id="maintenanceTransactionTypeDao" class="com.uprr.db.dao.spring.stats.MaintenanceTransactionTypeDAO" />
       <bean id="maintenanceItemTypeDao"        class="com.uprr.db.dao.spring.stats.MaintenanceItemTypeDAO"        />
       <bean id="maintenanceTransactionDao"     class="com.uprr.db.dao.spring.stats.MaintenanceTransactionDAO"     />
       <bean id="throwAdvice"                   class="com.uprr.aop.ThrowAdvice"                                   />
     
       <aop:config>
         <aop:pointcut id="allMethods" expression="execution(* *(..))" />
         <aop:advisor pointcut-ref="allMethods" advice-ref="throwAdvice" />
       </aop:config>
           
    </beans>
    But now we get a different error:
    Code:
    Exception in thread "main" 2008-06-09 09:47:36,060 [main] DEBUG - Creating implicit proxy for bean 'maintenanceTransactionTypeDao' with 0 common interceptors and 2 specific interceptors
    2008-06-09 09:47:36,060 [main] INFO - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@62937c: defining beans [dataSource,propertyConfigurer,developmentDataSource,maintenanceTransactionTypeDao,maintenanceItemTypeDao,maintenanceTransactionDao,throwAdvice,org.springframework.aop.config.internalAutoProxyCreator,allMethods,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0]; root of factory hierarchy
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'maintenanceTransactionTypeDao' defined in class path resource [spring-tns-api.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    	at Test.main(Test.java:23)
    Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
    	at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:67)
    	at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:106)
    	at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:473)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:348)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:309)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1342)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
    	... 14 more
    Google indicates "CGLIB" would require yet another jar in my classpath, which introduces even MORE complexity to the project. But the error message does mention an alternative of "proxy interfaces." How does one do proxy interfaces? What's the tradeoff between CGLIB & proxy interfaces? Is one significantly faster or easier to implement than the other?

  10. #20

    Unhappy Advisor Not Called

    I went ahead & bit the bullet & put CGLIB in the CLASSPATH, then ASM when it said it needed that, too. The app now runs to completion, except that the ThrowsAdvice is not executed, even after the code deliberately throws an exception to test it. Here's the log output (or as much as the forum software would accept). Can anyone see why the throws advisor is not called?

    Code:
    [org.springframework.context.support.ClassPathXmlApplicationContext@edf730]; startup date [Mon Jun 09 11:16:42 CDT 2008]; root of context hierarchy]
    2008-06-09 11:16:47,200 [main] DEBUG - Returning cached instance of singleton bean 'maintenanceTransactionTypeDao'
    2008-06-09 11:16:47,216 [main] DEBUG - Found exception handler method: public void com.uprr.aop.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)
    2008-06-09 11:16:47,294 [main] DEBUG - Executing SQL query [SELECT * FROM ctxd222.efm_ntwk_mtce_tran_types ORDER BY sort_ord_seq]
    2008-06-09 11:16:47,325 [main] DEBUG - Fetching JDBC Connection from DataSource
    2008-06-09 11:16:47,325 [main] DEBUG - Found exception handler method: public void com.uprr.aop.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)
    2008-06-09 11:16:47,325 [main] DEBUG - Found exception handler method: public void com.uprr.aop.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)
    2008-06-09 11:16:47,341 [main] DEBUG - Creating new JDBC Connection to [jdbc:oracle:thin:@dev091.oracle.uprr.com:1521:dev091]
    2008-06-09 11:16:48,185 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Falling back to [java.util.LinkedHashMap] for linked case-insensitive map
    2008-06-09 11:16:48,216 [main] DEBUG - Returning JDBC Connection to DataSource
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=1,desc=Create Track]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=2,desc=Modify Track]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=3,desc=Delete Track]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=4,desc=Remilepost Track]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=5,desc=Split Track]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=6,desc=Connect Tracks]
    2008-06-09 11:16:48,232 [main] INFO - TRAN_TYPE[sys#=7,desc=Disconnect Tracks]
    2008-06-09 11:16:48,232 [main] DEBUG - Found exception handler method: public void com.uprr.aop.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)
    2008-06-09 11:16:48,232 [main] DEBUG - Executing SQL query [SELECT tran_desc FROM ctxd222.efm_ntwk_mtce_tran_types WHERE tran_type_sys_nbr = 3]
    2008-06-09 11:16:48,232 [main] DEBUG - Fetching JDBC Connection from DataSource
    2008-06-09 11:16:48,232 [main] DEBUG - Creating new JDBC Connection to [jdbc:oracle:thin:@dev091.oracle.uprr.com:1521:dev091]
    2008-06-09 11:16:48,560 [main] DEBUG - Returning JDBC Connection to DataSource
    2008-06-09 11:16:48,591 [main] INFO - Transaction type # 3 = Delete Track
    Exception in thread "main" java.lang.Exception: Dummy exception to test AOP
    	at Test.testItemTypes(Test.java:32)
    	at Test.main(Test.java:26)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •