Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one to use: OpenSessionInViewInterceptor or OpenSessionInViewFilter?

I'm having a hard time deciding which "Open Session In View" to use: configuring OpenSessionInViewInterceptor using Spring MVC's interceptor with or configuring OpenSessionInViewFilter in web.xml's filter? From what I have researched, they do pretty much the same thing, but I'm trying to understand the difference and the usage of each type.

The biggest difference between the two is for folks who can't use a filter in web.xml (say, servlet 2.2 and earlier), their only option is to use OpenSessionInViewInterceptor. Somehow, I'm leaning towards the interceptor simply because I have to create a custom interceptor for my project, so I'm thinking of grouping all these "filters" in Spring MVC config file rather than having OpenSessionInViewFilter in web.xml and my custom interceptor in Spring MVC config file. It's really a lame way to decide which one to use, and my curiosity kills me here.

Can anyone share your thoughts about this? Which one do you guys use?

Thanks.

like image 270
limc Avatar asked Jan 20 '11 23:01

limc


2 Answers

As you say, the two are more or less equivalent. Spring provides them both so that you can pick the one that fits best with your existing application.

If you use Spring MVC, then it makes sense to use the interceptor, since it's easier to configure and better integrates with Spring MVC.

However, if you don't use Spring MVC, and only use Spring at the business-logic level, then the interceptor isn't really an option, and the filter becomes more appropriate. Because filters are not managed by Spring, they're harder to configure to integrate with Spring, but that's the trade-off.

like image 157
skaffman Avatar answered Nov 12 '22 03:11

skaffman


If you have any requests that are not going to go through a spring controller, i.e. legacy code that goes through a custom servlet, or jsp's that are hit directly, then the filter will cover those and make sure they get wrapped in a session. The interceptor will not cover those since those requests will not get picked up by the spring DispatcherServlet.

like image 5
Zeki Avatar answered Nov 12 '22 03:11

Zeki