Announcement Announcement Module
Collapse
No announcement yet.
ThrowsAdvice for a particular method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ThrowsAdvice for a particular method

    I've got a class that implements ThrowsAdvice with the following method:

    Code:
    public void afterThrowing(Method method, Object[] args, Object target, Throwable throwable)
    It is configured as

    Code:
        <bean id="defaultServiceTransactionProxy" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="transactionManager"/>
            </property>
            <property name="proxyTargetClass">
                <value>true</value>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean id="myServiceTransactionalTarget" parent="defaultServiceTransactionProxy">
            <property name="target">
                <bean class="MyService"/>
            </property>
        </bean>
    
        <bean id="myAuditAdvice" parent="defaultServiceTransactionProxy">
            <property name="target">
                <bean class="MyAuditAdvice"/>
            </property>
        </bean>
    
        <bean id="myService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
                <ref bean="myServiceTransactionalTarget"/>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>myAuditAdvice</value>
                </list>
            </property>
            <property name="proxyTargetClass">
                <value>true</value>
            </property>
        </bean>
    Names have been changed, obviously.

    This works great but does suffer from one problem. The advice will get
    applied if any method in MyService throws but I'd like it to apply to only a
    single method.

    I can test for the name in the afterThrowing() method via a call to
    method.getName() but I'm wondering if that is the best way to approach
    this.

    I am also wondering if the proxying of an already proxied class is OK.
    You'll notice that myServiceTransactionalTarget is proxied for transactional
    support. That proxy is then proxied for AOP support.

    It works but is it the best way to achive both declarative transaction
    support and AOP advice?

    Thanks,
    Kevin.

  • #2
    As far as only advising one method ... It looks like you need to combine your advice with a pointcut to create an advisor and configure that as your interceptor.

    I can't speak to the proxy of a proxy issue at this point.

    I only had time to scan your post, so if I missed something obvious, I appologize.

    Comment


    • #3
      Defining a pointcut was exactly what I had to do.
      I used the org.springframework.aop.support.RegexpMethodPointc utAdvisor
      with the pattern property specifying my method name.
      Works like a charm.

      Comment

      Working...
      X