Spring AOP代理

发布于 2024-10-05 21:12:41 字数 3251 浏览 3 评论 0原文

我的代码:-

<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

谢谢, 马诺伊

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-10-12 21:12:42

您使用的是哪个版本的 Spring?您进行代理的方式是较旧的方式。比较好的方式是使用注解或者纯POJO+XML的方式。您可以在此处查看 AOP 部分的简短介绍

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文