Results 1 to 5 of 5

Thread: MethodInterceptor Being Ignored

  1. #1
    Join Date
    Aug 2007
    Posts
    28

    Default MethodInterceptor Being Ignored

    I need help. I created an Advice class implementing MethodInterceptor to create a temporary workaround for a method returning bad data (band-aid fix, a "real" fix would have required a rewrite and a redesigned database schema).

    Rather than create a Pointcut, I simply started the method with a check of the method name (quick and dirty). I added the following lines to my applicationContext.xml file:


    Code:
        
    <bean id="equipmentSummaryReport"       class="com.whatever.reports.equipment.EquipmentSummaryReportImpl"/>
        <bean id="equipmentSummaryReportAdvice" class="com.whatever.aop.EquipmentTypeSummaryReportAdvice">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
        </bean>
        <bean id="esrAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="proxyTargetClass" value="true"/>
            <property name="interceptorNames">
                <list>
                    <idref bean="equipmentSummaryReportAdvice"/>
                </list>
            </property>
            <property name="beanNames" value="equipmentSummaryReport" />
    Unfortunately, the Advice/Interceptor never gets hit.

    Can anyone provide assistance?

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    If you create complete standalone test-case that illustrates the problem and post it here I take a look.

    Btw, why don't you use spring2 aop approach?

  3. #3
    Join Date
    Aug 2007
    Posts
    28

    Default

    Quote Originally Posted by denis.zhdanov View Post
    If you create complete standalone test-case that illustrates the problem and post it here I take a look.

    Btw, why don't you use spring2 aop approach?
    I'll try to post a test case as soon as I can (might be difficult to come up with something I can post), but I can't upgrade to Spring 2 at this time unless there is absolutely no other choice. The app was written for Spring 1.0 (and well before my company allowed us to use Java 5).

  4. #4
    Join Date
    Aug 2007
    Posts
    28

    Default

    Okay, here is the best I can do for a test case. I'm trying to intercept calls to the getTotals() method of a bean class named Report. Specifically, I'm trying to intercept calls to the getTotals() method.

    Code:
    package com.whatever;
    
    public class Report implements Serializable {
    
        private Map<Integer, Integer[]> totals;
    
        public Report();
    
        public Map<Integer, Integer[]> getTotals() {
            return this.totals;
    
        // etc, etc
        }
    Here is the current version of my applicationContext.xml file (relevant portions only):

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
            "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans default-autowire="byName">
    
        <bean id="reportTarget" class="com.whatever.Report" />
    
        <bean id="report" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
                <ref local="reportTarget" />
            </property>
            <property name="interceptorNames">
                <list>
                    <value>reportAdvisor</value>
                </list>
            </property>
        </bean>
    
        <bean id="reportAdvisor" class=class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice">
                <ref local="reportAdvice" />
            </property>
            <property name="pattern">
                <value>getTotals</value>
            </property>
        </bean>
    
        <!-- Bean requires datasource to be injected -->
        <bean id="reportAdvice" class="com.whatever.ReportAdvice">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
        </bean>
        </bean>
    </beans>
    Finally, a simple advice class:

    Code:
    package com.whatever;
    
    // imports, spring AOP and commons logging
    
    public class ReportAdvice implements MethodInterceptor {
    
        private Log log = LogFactory.getLog(ReportAdvice.class);
    
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    
            log.debug("invoke() method invoked!");
    
            // probably uneccessary using the regexp pointcut
            if (!methodInvocation.getMethod().getName().equals("getTotals")) {
                return methodInvocation.proceed();
            }
    
            // further code, make sure to return the right type
    
        }
    }
    As I mentioned before, I'm completely stumped why the method interceptor isn't being triggered.

  5. #5
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by DartmanX2 View Post
    ...
    As I mentioned before, I'm completely stumped why the method interceptor isn't being triggered.
    Your example works fine after couple of modifications:
    HTML Code:
        <bean id="report" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyTargetClass" value="true"/>
            <property name="target" ref="reportTarget"/>
            <property name="interceptorNames">
                <list>
                    <value>reportAdvisor</value>
                </list>
            </property>
        </bean>
    
        <bean id="reportAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice">
                <ref local="reportAdvice"/>
            </property>
            <property name="pattern" value=".*getTotals"/>
        </bean>

Posting Permissions

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