2010-12-06 6 views
0

私のコード: -春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メソッドを実行することはありません。何が起こっているのか、それを呼び出す方法を教えてください。ここで

は、テストクラスの出力である: -

$ Proxy4 15.5

おかげで、春のバージョンを使用している Manojさん

答えて

0

?プロキシをやっているやり方は古い方法です。より良い方法は、アノテーションまたは純粋なPOJO + XMLの方法を使用することです。 AOPセクションの短い紹介を確認することができますhere

+0

私はSpring3を使用しています、私はinroundのinvokeメソッドで呼び出される実際のインスタンスを制御したいと思います。なぜこの古いスタイルを使用しているのですか? LoggingAroundAdviceでは、useStubのflag.Butに応じて使用する実装がチェックされていますが、Springがinvoke()メソッドを呼び出さない理由はわかりません。 – Manoj

+0

getTargetメソッドを使用してProceedingJoinPointから取得できません。 – lalit

+0

また、あなたがしようとしていることを伝えることができれば、それはよりよく答えるのに役立ちます。私は、アドバイスコンストラクタにスタブと実際の電卓の両方を渡してアドバイスをしたいのはなぜか分かりません。また、あなたのテストサービスは、同じ構成XMLの中に座っています。あなたが目的を示すためだけにそれをしたのであれば、それは問題ありません。 – lalit