2013-05-27 1 views
0

J'ai une application Spring MVC, et j'étais assez confiant de comprendre toutes les configurations que j'ai configurées, cependant, dans un exercice récent, je suis passé à une configuration basée sur des annotations pures (sauf la configuration de sécurité) et j'ai vu un changement dans le comportement de l'application qui m'a fait réfléchir deux fois sur quelques points. Le principal changement de comportement, c'est qu'il semble que mon ancienne application exécutait un modèle de type OpenSessionInView - car plusieurs de mes objets de domaine étaient des éléments de chargement paresseux, mais ces éléments pouvaient encore être accessibles en dehors de la couche transactionnelle/service classes (par exemple dans mes contrôleurs) - après avoir basculé vers des annotations, cela échoue maintenant comme vous le feriez avec des erreurs de chargement paresseux car la session n'est pas ouverte dans ma couche de contrôleur.Comprendre la configuration Spring MVC

Ci-dessous la configuration que j'ai utilisée - quelqu'un peut-il expliquer quelle partie de ceci est responsable du comportement de type OpenSessionInView que mon application affichait précédemment?

App fichier de configuration de contexte:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xmlns:cloud="http://schema.cloudfoundry.org/spring" xmlns:cache="http://www.springframework.org/schema/cache" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://schema.cloudfoundry.org/spring 
    http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache.xsd"> 


    <context:property-placeholder location="classpath*:META-INF/spring/*.properties" /> 
    <context:spring-configured /> 
    <context:annotation-config /> 
    <mvc:annotation-driven /> 
    <aop:aspectj-autoproxy proxy-target-class="true" /> 
    <context:component-scan base-package="com.tmm.enterprise.adapter"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.service"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.helper"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.util"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.repository"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.core.dao"></context:component-scan> 
    <context:component-scan base-package="com.tmm.enterprise.security.dao"></context:component-scan> 


    <!--Http client --> 
    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"> 
     <property name="state" ref="httpState" /> 
    </bean> 

    <!--Http state --> 
    <bean id="httpState" factory-bean="httpStateFactory" factory-method="buildState"/> 

    <!--Http client --> 
    <bean id="httpClientFactory" 
     class="org.springframework.http.client.CommonsClientHttpRequestFactory"> 
     <constructor-arg ref="httpClient" /> 
    </bean> 

    <!--RestTemplate --> 
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> 
     <constructor-arg ref="httpClientFactory" /> 
    </bean> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     lazy-init="default" autowire="default"> 
     <property name="driverClass" value="${database.driverClassName}" /> 
     <property name="jdbcUrl" value="${database.url}" /> 
     <property name="user" value="${database.username}" /> 
     <property name="password" value="${database.password}" /> 
    </bean>  


    <bean class="org.springframework.orm.jpa.JpaTransactionManager" 
     id="transactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <bean id="applicationController" class="com.tmm.enterprise.service.ApplicationService"></bean> 

    <bean id="socialConfig" 
     class="com.tmm.enterprise.configuration.SocialConfiguration"></bean> 
    <bean id="cachingConfig" 
     class="com.tmm.enterprise.configuration.CachingConfiguration"></bean> 

    <tx:annotation-driven mode="aspectj" 
     transaction-manager="transactionManager" proxy-target-class="true" /> 


    <bean 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
     id="entityManagerFactory"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="persistenceUnitName" value="persistenceUnit" /> 
    </bean> 

    <bean id="geocoder" class="com.google.code.geocoder.Geocoder" /> 

    <bean id="validatorFactory" class="javax.validation.Validation" 
     factory-method="buildDefaultValidatorFactory" /> 

    <bean id="validator" factory-bean="validatorFactory" 
     factory-method="getValidator" /> 
</beans> 

MVC Config:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <!-- The controllers are autodetected POJOs labeled with the @Controller annotation. --> 
    <context:component-scan base-package="com.tmm.enterprise.controller" use-default-filters="false"> 
     <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </context:component-scan> 

    <!-- Turns on support for mapping requests to Spring MVC @Controller methods 
     Also registers default Formatters and Validators for use across all @Controllers --> 
    <mvc:annotation-driven/> 

    <mvc:resources mapping="/resouces/**" location="/styles, /images, /javascript, /libs" /> 

    <!-- a higher value meaning greater in terms of sorting. --> 
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3"> 
     <property name="alwaysUseFullPath" value="true" /> 
    </bean> 

    <!-- register "global" interceptor beans to apply to all registered HandlerMappings --> 
    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/> 
    </mvc:interceptors> 

    <!-- selects a static view for rendering without the need for an explicit controller --> 
    <mvc:view-controller path="/login"/> 
    <!--mvc:view-controller path="/index"/--> 
    <mvc:view-controller path="/uncaughtException"/> 
    <mvc:view-controller path="/resourceNotFound"/> 
    <mvc:view-controller path="/dataAccessFailure"/> 
    <mvc:view-controller path="/jobs" view-name="jobsPage"/> 
    <mvc:view-controller path="/applications" view-name="jobsPage"/> 
    <mvc:view-controller path="/users" view-name="usersPage"/> 
    <mvc:view-controller path="/companies" view-name="companiesPage"/> 
    <mvc:view-controller path="/tos" view-name="tos"/> 
    <mvc:view-controller path="/about" view-name="about"/> 
    <mvc:view-controller path="/privacypolicy" view-name="privacyPolicy"/> 
    <mvc:view-controller path="/sitemap.xml" view-name="sitemap"/> 

    <!-- Resolves logical view names returned by Controllers to Tiles; a view 
     name to resolve is treated as the name of a tiles definition --> 
    <bean class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" id="tilesViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/> 
    </bean> 

    <!-- Configures the Tiles layout system --> 
    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/layouts/layouts.xml</value> 
       <!-- Scan views directory for Tiles configurations --> 
       <value>/WEB-INF/views/**/views.xml</value> 
      </list> 
     </property> 
    </bean> 

    <!-- Resolves localized messages*.properties and application.properties files in the application to allow for internationalization. 
     The messages*.properties files translate Roo generated messages which are part of the admin interface, the application.properties 
     resource bundle localizes all application specific messages such as entity names and menu items. --> 
    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/> 

    <!-- store preferred language configuration in a cookie --> 
    <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/> 

    <!-- resolves localized <theme_name>.properties files in the classpath to allow for theme support --> 
    <bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/> 

    <!-- store preferred theme configuration in a cookie --> 
    <bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/> 

    <!-- This bean resolves specific types of exceptions to corresponding logical - view names for error views. 
     The default behaviour of DispatcherServlet - is to propagate all exceptions to the servlet container: 
     this will happen - here with all other types of exceptions. --> 
    <bean class="com.tmm.enterprise.controller.exception.NerdExceptionHandler" p:defaultErrorView="uncaughtException"> 
     <property name="exceptionMappings"> 
      <props> 
       <prop key=".DataAccessException">dataAccessFailure</prop> 
       <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop> 
       <prop key=".TypeMismatchException">resourceNotFound</prop> 
       <prop key=".MissingServletRequestParameterException">resourceNotFound</prop> 
      </props> 
     </property> 
    </bean> 


    <!-- Controllers --> 
    <bean id="connectController" class="com.tmm.enterprise.controller.ConnectController"></bean> 

    <!-- allows for integration of file upload functionality --> 
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/> 

    <bean id="ajaxViewResolver" 
     class="com.tmm.enterprise.views.AjaxViewResolver"> 
     <property name="ajaxView"> 
      <bean class="com.tmm.enterprise.views.AjaxView" /> 
     </property> 
     <property name="ajaxPrefix" value="ajax_"></property> 
    </bean> 

    <bean id="editableViewResolver" 
     class="com.tmm.enterprise.views.EditableViewResolver"> 
     <property name="editableView"> 
      <bean class="com.tmm.enterprise.views.EditableView" /> 
     </property> 
     <property name="editablePrefix" value="editable_"></property> 
    </bean> 
</beans> 

Répondre

1

Le comportement OpenSessionInView (Hibernate)/OpenEntityManagerInView (APP) fait souvent partie d'un filtre défini dans web.xml, mais peut également être fait en utilisant un intercepteur, comme décrit dans this answer. Vous pouvez utiliser OpenEntityManagerInViewInterceptor pour JPA.

<mvc:interceptors> 
    <bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> 
     <property name="sessionFactory"> 
      <ref local="sessionFactory" /> 
     </property> 
    </bean> 
</mvc:interceptors> 
+0

aghhh - être un idiot à nouveau alors .. était passé par tous les détails de ces deux fichiers de configuration et était sûr qu'il n'y avait rien d'ouvrir la session dans le filtre de vue, il .. avait complètement oublié ma config web.xml ! – rhinds

+0

Donc, j'ai toujours la configuration config dans mon fichier web.xml - savez-vous pourquoi cette config pourrait être ignorée maintenant j'ai basculé les autres configs vers des annotations pures? – rhinds

+1

Ah. Vous avez également basculé vers JPA, de sorte que le OpenEntityManagerInViewFilter/Interceptor sera ce dont vous avez besoin – NealeU

Questions connexes