JAVA 大牛们,小弟我遇到一个很古怪的问题。不知道是怎么回事。
先描述下问题把。我用的struts2+spring2.5 做的项目。我在页面端有一个AJAX 调用。我的SPRING 后置通知配置在service 层。当然我的AJAX 也调用了。现在的问题是我写的后置通不执行。不知道怎么回事。我粘上部分代码希望大家能帮我解决下。
package cn.com.nuoter.playbar.businessAfter; import java.lang.reflect.Method; import java.lang.reflect.Type; import org.springframework.aop.AfterReturningAdvice; public class BusinessAfter implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("After.afterReturning()"); System.out.println("return value: " + returnValue); System.out.println("method name: " + method.getName()); Type[] type = method.getGenericParameterTypes(); for (int i = 0; i < type.length; i++) { System.out.println(type[i].toString() + ": " + args[i]); } System.out.println("target: " + target.toString()); // 返回值更改 // 无效 returnValue = new String("after"); System.out.println(""); } }
下面是我的配置文件
<beans> <bean id="after" class="cn.com.nuoter.playbar.businessAfter.BusinessAfter"/> <bean id="aop" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" value="cn.com.nuoter.playbar.business.service.BusinessPageService" /> <property name="interceptorNames"> <list> <value>after</value> </list> </property> <property name="target" ref="busService"> </property> </bean> </beans>
启动的时候不包任何错误。
当我用ajax调用的时候。没有任何错误。就是上面发的java 代码不执行。不知道怎么回事。希望大家帮我看看。谢谢了。
所以说嘛,就是你的action配置有问题
应该这样
看看这里,你把busService封装成一个新的叫aop的bean,但你的busPageAction却还在用busService,应该用aop这个bean才行的
<bean id="aop" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="cn.com.nuoter.playbar.business.service.BusinessPageService" />
<property name="interceptorNames">
<list>
<value>after</value>
</list>
</property>
<property name="target" ref="busService">
</property>
</bean>
AJAX调用的是通过Spring得到的bean?
你确认你应用里是使用的aop这个bean,别你还是把after这个bean注入到action里
lovewhzlq 说的是应将aop这个bean注入到service层,不是“把after 注入到service层”
把你注入的配置粘出来看
我是说配置action的配置粘出来
[code="xml"]
<bean id="after" class="cn.com.nuoter.playbar.businessAfter.BusinessAfter"/>
<bean id="aop" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="cn.com.nuoter.playbar.business.service.BusinessPageService" />
<property name="interceptorNames">
<list>
<value>after</value>
</list>
</property>
<property name="target">
<bean class="xxx.BusinessPageServiceImpl" />
</property>
</bean>
[/code]
改成上面的写法,把busService的bean定义删了。以免误用,getBean时不要用busService这个id,用aop这个id。
应该是把aop这个配置好了后置通知的service bean注入到action中,这样请求进action调用其中的aop这个bean的方法时候就可以正常调用advice中的代码了
顺便说句,这个aop名字起的很奇怪,容易引起误解
[code="xml"]
[/code]
[code="xml"]
[/code]
是一个Proxy,代理了busService这个bean,并且加入了interceptor,因为你要用aop这个bean,才能有增强。
而叫busService不知道Advice,它只是一个普通的,没有增强的bean。
建议ls看看Spring的书,
或者《Expert One-on-One J2EE Development without EJB》。