2010-12-06 100 views
0

我的代碼: -Spring AOP的代理

<context:annotation-config/> 
    <bean id="arthmeticCalculator" class="com.manoj.aop.test.CalculatorImpl" lazy-init="true"/> 
    <bean id="stubCalculator" class="com.manoj.aop.test.StubCalculator" lazy-init="true"/> 
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
     <property name="beanNames"> 
     <list> 
      <value>*Calculator</value> 
     </list> 
     </property> 
     <property name="interceptorNames"> 
     <list> 
      <value>methodNameAdvisor</value> 
     </list> 
     </property> 
    </bean> 
    <bean id="methodNameAdvisor" 
     class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 
    <property name="mappedNames"> 
     <list> 
     <value>add</value> 
     <value>sub</value> 
     </list> 
    </property> 
    <property name="advice" ref="loggingAroundAdvice" /> 
    </bean> 
    <bean id="loggingAroundAdvice" class="com.manoj.aop.test.LoggingAroundAdvice"> 
     <constructor-arg><ref bean="arthmeticCalculator"/></constructor-arg> 
     <constructor-arg><ref bean="stubCalculator"/></constructor-arg> 
     <constructor-arg><value>false</value></constructor-arg> 
    </bean> 
    <bean id="testService" class="com.manoj.aop.test.TestService"> 
    <!-- 
     <property name="arthmeticCalculator" ref="arthmeticCalculator"/> 
    --> 
    </bean> 

Java代碼:

package com.manoj.aop.test; 

import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.beans.factory.annotation.Value; 

public class LoggingAroundAdvice implements MethodInterceptor{ 


     Calculator actualCalculator; 
     Calculator stubCalculator; 
     boolean useStub; 



public LoggingAroundAdvice(Calculator actualCalculator, Calculator stubCalculator, boolean useStub) { 
    this.actualCalculator = actualCalculator; 
    this.stubCalculator = stubCalculator; 
    this.useStub = useStub; 
    } 



public Object invoke(MethodInvocation methodInvocation) throws Throwable { 
    System.out.println("Around Invoice called"); 
    Calculator calc = useStub ? stubCalculator: actualCalculator; 
    System.out.println(calc.getClass().getName()); 
    Object result = methodInvocation.getMethod().invoke(calc, methodInvocation.getArguments()); 
    return result; 
} 

} 

import org.springframework.beans.factory.annotation.Autowired; 

public class TestService { 

@Autowired 
    private Calculator arthmeticCalculator; 


    public void test(){ 
     System.out.println(arthmeticCalculator.getClass().getName()); 
     System.out.println(arthmeticCalculator.add(5, 10.5)); 
    } 



} 

對不起傢伙,我不知道如何在這個編輯的文字格式, 我的問題是: -

Spring正在爲該類創建代理,但從未執行Around建議的Invoke方法。有人可以告訴我怎麼回事,以及如何使它調用invoke方法?

下面是測試類的輸出: -

$ Proxy4 15.5

感謝, 馬諾

回答

0

Spring的版本是您使用?你做代理的方式是舊的方式。更好的方法是使用註釋或純POJO + XML方式。你可以查看AOP部分的簡短介紹here

+0

我正在使用Spring3 ,,我想控制實際的實例,它將在Around invice的invoke方法中調用,這就是爲什麼我使用這種舊樣式。在LoggingAroundAdvice中,我正在檢查使用哪個實現取決於useStub標誌。但我不知道爲什麼Spring不調用invoke()方法? – Manoj 2010-12-06 09:19:37