Spring 中的拦截器与方面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3599976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:07:12  来源:igfitidea点击:

Interceptors vs Aspects in Spring?

springinterceptoraspects

提问by Amr Faisal

I am trying to use interceptors in Spring. I want to implement an interceptor on some methods to handle specific logic when these methods are called. I also want to be apart from using web framework, as am tending to use Spring as back end, without any headers.

我正在尝试在 Spring 中使用拦截器。我想在一些方法上实现一个拦截器来处理调用这些方法时的特定逻辑。我也想除了使用 web 框架之外,因为我倾向于使用 Spring 作为后端,没有任何标题。

After searching, I think spring approach is called Aspects, could you please mention best practice to do this?

经过搜索,我认为spring方法称为Aspects,您能否提及执行此操作的最佳实践?

回答by skaffman

In Spring there are two different constructs that get called "interceptors".

在 Spring 中,有两种不同的结构被称为“拦截器”。

First, there are Handler Interceptors, which are part of the Spring MVC framework, and allow you to add interceptor logic to web requests. I suspect this is what you meant when you said "interceptors"

首先,有Handler Interceptors,它是 Spring MVC 框架的一部分,允许您向 Web 请求添加拦截器逻辑。我怀疑这就是你说“拦截器”时的意思

Then you have Method Interceptors, which are part of the Spring AOPframework. These are much more general mechanism than Handler Interceptors, but also potentially more complex. In AOP terminology, such interceptors provide a means of coding the "aspects" you're talking about. The

然后是方法拦截器,它是Spring AOP框架的一部分。这些是比处理程序拦截器更通用的机制,但也可能更复杂。在 AOP 术语中,此类拦截器提供了一种对您正在谈论的“方面”进行编码的方法。这

AOP aspects are a complex subject. If you're interested, I suggest that you skim over the stuff in the Spring AOP manualand get a feel for the terminology, and what it can and can't do for you.

AOP 方面是一个复杂的主题。如果您有兴趣,我建议您浏览Spring AOP 手册中的内容并了解术语,以及它可以为您做什么和不能做什么。