I am using the Hibernate/Spring/Struts architecture and configure to use the OpenSessionInViewFilter of Spring. However, I find that this filter does not take effect at all, when accessing the page, lazy still reports the session closed error. All configurations and related code are checked, but no problems are found. After debugging, it is found that the Session opened by the application and OpenSessionInView Filter are not the same, so the OpenSessionInView mode does not take effect, but why do they not use the same Session?
Check the source code of Spring and find the root cause of the problem:
Generally, Spring configuration is initialized in Web applications. We will configure a Listener in web. xml, that is:
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>
If Struts is used, configure a Spring plugin: ContextLoaderPlugIn In the Struts configuration file struts. xml.
<Plug-in className = "org. springframework. web. struts. ContextLoaderPlugIn">
<Set-property = "contextConfigLocation" value = "/WEB-INF/applicationContext. xml"/>
</Plug-in>
In fact, the functions of ContextLoaderListener and ContextLoaderPlugIn overlap. They initialize Spring configurations. Therefore, if you do not intend to use OpenSessionInView, you do not need to configure ContextLoaderListener in web. xml.
Now, you need to integrate Struts with Spring and OpenSessionInView!
Since the ContextLoaderListener and ContextLoaderPlugIn functions overlap, both initialize Spring and you should not perform two initialization times. Therefore, you should not use both of them at the same time. You can only select one, because you need to integrate Struts, therefore, you can only use ContextLoaderPlugIn.
However, ContextLoaderListener and ContextLoaderPlugIn are very conflicting!
ContextLoaderListener initializes the spring configuration and stores it in the ServletContext object:
ServletContext. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this. context );
Note that the key of the saved object is WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!
However, ContextLoaderPlugIn initializes the spring configuration and stores it in the ServletContext object:
String attrName = getServletContextAttributeName ();
GetServletContext (). setAttribute (attrName, wac );
The attrName and WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE are different!
If the names are different, the problem is not big. You can still use ContextLoaderPlugIn with confidence. But when you use OpenSessionInView, which key does OpenSessionInViewFilter use to retrieve spring configurations?
WebApplicationContext wac = WebApplicationContextUtils. getRequiredWebApplicationContext (getServletContext (););
Apparently, OpenSessionInViewFilter uses the key WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE to get spring configuration!
The name of ContextLoaderPlugIn for saving spring configurations is attrName;
The name of the ContextLoaderListener to save the spring configuration is WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
OpenSessionInView obtains spring configuration according to the name WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!
Your application obtains the spring configuration according to the attrName!
Therefore, the OpenSessionInView mode is invalid!
Solution:
Modify the ContextLoaderPlugIn code and add a line of code in getServletContext (). setAttribute (attrName, wac:
GetServletContext (). setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac );
This article from "want to span, pay more" blog, please be sure to keep this source http://andywuchuanlong.blog.51cto.com/4898493/1305320