Spring 3 MVC @Controller with AOP interceptors?

こ雲淡風輕ζ 提交于 2019-11-27 19:11:02

From the Spring MVC Reference:

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.

Granted, this note is well hidden :-)

I ran into the same issue and found out the solution.

Indeed your controller (annotated by @Controller) and your aspects (annotated by @Aspect) should be in the same Spring context.

Usually people define their controllers in the dispatch-servlet.xml or xxx-servlet.xml and their service beans (including the aspects) in the main applicationContext.xml. It will not work.

When Spring initializes the MVC context, it will create a proxy for your controller but if your aspects are not in the same context, Spring will not create interceptors for them.

The above ssertion does not depend

  • on the way your declare your controllers/aspects (by manual XML declaration or annotation style)
  • on the proxying style you choose (JDK proxy or CGLIB)

I've tested all the combinations and they all work as long as the controller & aspects are in the same Spring context

My best guess without doing some serious digging is because Spring's AOP mechanism that you are using is wrapping the target classes in proxy classes which end up loosing their annotation or the original annotation gets dropped after weaving.

I am sure there is a better answer and I'll expand on mine as I think of a better more clear way to present it.

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