Java springaop代理
我的代码:-Java springaop代理,java,spring,spring-aop,Java,Spring,Spring 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"/> <b
<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>
对不起,伙计们,我不知道如何在这个编辑器中格式化文本,
我的问题是:-
Spring正在为类创建代理,但从未执行Around通知的Invoke方法。有人能告诉我发生了什么,以及如何让它调用invoke方法吗
以下是测试类的输出:-
$Proxy4
15.5
谢谢,
Manoj您使用的是哪个版本的Spring?您执行代理的方式是旧的方式。更好的方法是使用注释或纯POJO+XML方式。您可以查看AOP部分的简短介绍我使用的是Spring3,我希望控制将在Around invice的invoke方法中调用的实际实例,这就是我使用这种旧样式的原因。在LoggingAroundAdvice中,我正在根据useStub标志检查要使用哪个实现。但我不知道为什么Spring不调用invoke()方法?您不能使用getTarget方法从ProceedingJoinPoint获得这个结果。另外,如果您能告诉我您正在尝试做什么,这将有助于更好地回答这个问题。我不知道为什么要通过在通知构造函数中传递存根和实际计算器来生成通知。另外,您的测试服务位于相同的配置XML中,这听起来像是反模式。若您这样做只是为了显示目的,那个么就没问题了。好吧,在我的应用程序中,一些服务还并没有实现。这些服务调用后端系统获取数据,并最终向我返回VO。现在这些系统是第三方系统,还没有准备好提供实际数据。因此,我尝试使用AOP存根这些服务。因此,当我从代码中调用服务时,我将获得一些存根数据,并且该决定由“isStub”标志做出,该标志可以放在properties文件中。但问题是invoke方法从未被调用。我写的代码有一些严重错误,我无法发现。
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));
}
}