简体   繁体   中英

Spring AOP proxy

My code:-

<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 Code:

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));
     }



}

Sorry guys I dont know how to format text in this editor, My problem is :-

Spring is creating proxy for the class but never execute the Invoke method of the Around advice. Can some body please tell me whats going on and how to make it call the invoke method?

Here is the output of test class:-

$Proxy4 15.5

Thanks , Manoj

Which version of Spring are you using? The way you are doing proxy is the older way. The better way is to use the annotation or pure POJO+XML way. You can check a short introduction on AOP section here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM