Using @Before aspect on @Controller methods with Spring doesn't work

喜你入骨 提交于 2019-12-11 04:06:56

问题


I have a web-app using Spring 3, where controllers are annotated with @Controller. The public methods of the controllers are annotated with @RequestMapping. This works perfectly fine.

Now I want to do a check before invoking any of the public methods in the controllers. I've created a @Before aspect using a Pointcut expression that selects all controller methods annotated with @RequestMapping. I've registered the aspect using

<aop:aspectj-autoproxy>
    <aop:include name="myAspect"/>
</aop:aspectj-autoproxy>

The problem is that when I've started the application and do a request for some URL handled by one of my controllers, I get the following error-message:

"No adapter for handler XXX: Does your handler implement a supported interface like Controller?"

So the controllers don't work anymore. Does anyone have an idea on how to fix this?


回答1:


Sean Patrick Floyd is right. There is an other way: switching form Spring Proxy AOP to CGILib.

From the Spring Reference:

It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.

http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-autoproxy-force-CGLIB

To force the use of CGLIB proxies set the value of the proxy-target-class attribute of the <aop:config> element to true: <aop:config proxy-target-class="true">

To force CGLIB proxying when using the @AspectJ autoproxy support, set the 'proxy-target-class' attribute of the <aop:aspectj-autoproxy> element to true: <aop:aspectj-autoproxy proxy-target-class="true"/>

BTW: I recommend to use AspectJ instead or Spring Proxy CGILib AOP./




回答2:


Note
When using controller interfaces (e.g. for AOP proxying), make sure to consistently put all your mapping annotations - such as @RequestMapping and @SessionAttributes - on the controller interface rather than on the implementation class.

Source: Spring Reference > Web MVC Framework > Implementing Controllers



来源:https://stackoverflow.com/questions/5578354/using-before-aspect-on-controller-methods-with-spring-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!