繁体   English   中英

Spring AOP代理

[英]Spring AOP proxy

我的代码:-

<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方法吗?

这是测试类的输出:

$代理4 15.5

谢谢,马诺

您正在使用哪个版本的Spring? 您执行代理的方式是较旧的方式。 更好的方法是使用注释或纯POJO + XML的方法。 您可以在此处查看有关AOP的简短介绍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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