※当サイトの記事には、広告・プロモーションが含まれます。

Spring Web MVCのInterceptorについて調べてみた

www.zdnet.com

⇧ Asiaはまだ未対象ですね...

Spring Web MVCのInterceptorについて調べてみた

stackoverflowの情報で、

stackoverflow.com

⇧ Interceptorなる仕組みが出てきたのですが、

meetup-jp.toast.com

⇧ FilterとInterceptorの違いについては、上記サイト様が詳しいです。

ちなみに、今は使われることが無いと思われるApache StrutsというフレームワークでのHttpのRequestが処理されるアーキテクト図によると、

struts.apache.org

If the ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to the ActionProxy. The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xml file). Next, the ActionProxy creates an ActionInvocation, which is responsible for the command pattern implementation. This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

https://struts.apache.org/core-developers/big-picture

⇧ Interceptorを使ってるっぽい。

Apache StrutsのInterceptorはSpring FrameworkのSpring Web MVCのInterceptorとは異なるとは思うのけど、Java EE(Enterprise Edition)の標準APIで、

docs.oracle.com

Contains annotations and interfaces for defining interceptor methods, interceptor classes and for binding interceptor classes to target classes.

https://docs.oracle.com/javaee/7/api/javax/interceptor/package-summary.html

Interceptor methods

An interceptor method may be defined on a target class itself or on an interceptor class associated with the target class. AroundConstruct interceptor may be defined only on an interceptor class.

https://docs.oracle.com/javaee/7/api/javax/interceptor/package-summary.html

⇧ javax.interceptorってパッケージがあるっぽいんだけど、何か関係あるんですかね?

まぁ、「javax.interceptor」と「org.springframework.web.servlet.HandlerInterceptor」の関係はよく分からないのですが、Spring Web MVCにおいて、「org.springframework.web.servlet.HandlerInterceptor」インターフェイスをimplementsしたクラスを実装すれば、Controllerのメソッドが呼ばれる前に実施させたい処理を割り込ませることができるようです。

■InceptorHandlerのイメージ図

⇧ 上図のようにControllerクラスのメソッドをたらい回しにするケースは実際には無いと思うのだけど、イメージ的には、Controllerのメソッドが呼ばれる度に、InceptorHandlerをImplementsしたクラスが呼ばれる感じですかね?

なので、FilterよりもInceptorHandlerをimplementsしたクラスが呼ばれる回数が多くなるケースが有り得ますと。

それにしても、Spring FrameworkのSpring Web MVC以外でもInterceptor使ってるのかよく分からないけど、

docs.spring.io

⇧ Interceptorの種類がいろいろあるっぽい...

そもそも、Spring FrameworkにおけるInterceptorの定義が分からんので、「org.springframework.web.servlet.HandlerInterceptor」がInterceptorの仕組みを使ってると言えるのかが分からんのよね...

Wikpediaさんによりますと、

In the field of software development, an interceptor pattern is a software design pattern that is used when software systems or frameworks want to offer a way to change, or augment, their usual processing cycle. 

https://en.wikipedia.org/wiki/Interceptor_pattern

Key aspects of the pattern are that the change is transparent and used automatically. In essence, the rest of the system does not have to know something has been added or changed and can keep working as before. To facilitate this, a predefined interface for extension has to be implemented, some kind of dispatching mechanism is required where interceptors are registered (this may be dynamic, at runtime, or static, e.g. through configuration files) and context objects are provided, which allow access to the framework's internal state.

https://en.wikipedia.org/wiki/Interceptor_pattern

⇧ Interceptor patternなるデザインパターンがあるようなのですが、

Uses and context

An example of implementation of this pattern is the javax.servlet.Filter interface, which is part of Java Platform, Enterprise Edition.

https://en.wikipedia.org/wiki/Interceptor_pattern

⇧ まさかのjavax.servlet.Filterが実装例という...

う~む、結局、Spring FrameworkのSpring Web MVCにおけるInterceptorの定義がよく分からん...

毎度モヤモヤ感が半端ない...

今回はこのへんで。