2010-12-06 1 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> 

자바 코드 :이 편집기에서 텍스트를 포맷하는 방법을 잘 모릅니다

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 advice의 Invoke 메소드를 실행하지 않습니다. 어떤 시체가 어떻게 진행되는지 그리고 호출 메소드를 호출하는 방법을 알려주시겠습니까?

$ Proxy4 15.5

감사합니다, 마노

답변

0

사용하는 봄의 버전 - : 여기

는 테스트 클래스의 출력입니까? 프록시를 수행하는 방법은 이전 방법입니다. 더 좋은 방법은 주석 또는 순수 POJO + XML 방식을 사용하는 것입니다. AOP 섹션에서 짧은 소개를 확인할 수 있습니다. here

+0

Spring3을 사용하고 있습니다. Around invice의 호출 메소드에서 호출 될 실제 인스턴스에 대한 제어권을 갖고 싶습니다. 왜 내가이 이전 스타일을 사용하고 있는지. LoggingAroundAdvice에서 useStub 플래그에 따라 어떤 구현을 사용하는지 확인하고 있습니다.하지만 Spring이 invoke() 메소드를 호출하지 않는 이유는 무엇입니까? – Manoj

+0

getTarget 메서드를 사용하여 ProceedingJoinPoint에서 가져올 수 없습니까? – lalit

+0

또한 당신이 무엇을하려고하는지 말할 수 있다면 그것을 더 잘 대답하는 데 도움이 될 것입니다. 왜 조언 생성자에서 스텁과 실제 계산기를 모두 전달하여 조언을하고 싶은지 잘 모르겠습니다. 또한 테스트 서비스는 동일한 구성 XML에 있으며, 이는 나에게 안티 패턴처럼 들립니다. 당신이 목적을 보여주는 목적으로 만 그것을했다면, 그것은 괜찮습니다. – lalit