首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring AOP代理

Spring AOP代理
EN

Stack Overflow用户
提问于 2010-12-06 17:03:50
回答 1查看 1K关注 0票数 0

我的代码:-

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
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方法。有人能告诉我这是怎么回事吗?怎么让它调用调用方法?

以下是test类的输出:

$Proxy4 15.5

谢谢,Manoj

EN

回答 1

Stack Overflow用户

发布于 2010-12-06 17:09:22

您使用的是哪个版本的Spring?你做代理的方式是比较老的方式。更好的方法是使用注释或纯POJO+XML方式。您可以查看AOP部分here上的简短介绍。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4364702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档