2012-02-10 3 views
4

私は2つの質問、Spring MVCのアプリケーションでのWebアプリケーションのコンテキスト/ルートアプリケーションコンテキストとトランザクションマネージャのセットアップ

を持っていた、のContextLoaderListenerを持つことの目的は何ですか?以下は

は、web.xml内の私のエントリである、 すべてのMVC豆は、サーブレットのcontext.xml すべてのデータベースとのアノテーションベースのトランザクション管理はapplicationContext.xmlをで定義されていると私は、コンテナ管理のトランザクションを使用している中で定義されていますJBoss

私がapplicationContext.xmlをDispatcherServletに対して強調表示するように渡すと、trasaction managerがうまく動作します。しかし、私は、Spring MVCコンテキスト情報をDispatcherServletに渡すだけでよいと考えました。

applicationContext.xmlを削除すると、トランザクションマネージャは機能しなくなりますか?私はコンテキストファイルを管理する最良の方法は何か混乱していますか?

のWeb.xml

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>  
     /WEB-INF/config/applicationContext.xml 
     /WEB-INF/config/spring-mail.xml 
    </param-value> 
</context-param> 
<context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>messages</param-value> 
</context-param> 


<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/config/servlet-context.xml 
      ***/WEB-INF/config/applicationContext.xml*** 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

applicationContext.xmlを

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

<!-- registers all of Spring's standard post-processors for annotation-based configuration --> 
<context:annotation-config /> 

<jee:jndi-lookup id="dataSource" jndi-name="java:OracleDS"/> 


<tx:annotation-driven/> 
<tx:jta-transaction-manager/> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="configLocation" 
       value="classpath:com/common/model/config/sqlmapconfig.xml"/> 
</bean> 

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
    <constructor-arg ref="sqlSessionFactory"/> 
</bean> 

サーブレットのcontext.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    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"> 

<!-- Scans the classpath of this application for @Components to deploy as beans --> 
<context:component-scan base-package="com.xxxx"/> 

<!-- Configures the @Controller programming model --> 
<mvc:annotation-driven/> 

<!-- Configures Handler Interceptors --> 
<mvc:interceptors> 
    <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> 
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> 
    <!--Register Request/Response Interceptor--> 
    <bean class="com.xxx.common.auditor.RequestInterceptor"/> 
    <!-- <bean class="com.xxx.common.interceptor.UserAuthenticationInterceptor"/>--> 
</mvc:interceptors> 

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> 

<!-- Application Message Bundle --> 
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="/WEB-INF/messages/messages"/> 
    <property name="cacheSeconds" value="0"/> 
</bean> 


<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/views/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>/WEB-INF/xxxx.properties</value> 
    </property> 
</bean> 

あなたのためのおかげで助けて!私はその引用を長く知っていますが、自分自身をよりよく理解したいと思っていました。

答えて

13

the documentationで説明したように、すべてのディスパッチャーサーブレットには独自のアプリケーションコンテキストがあり、通常はコントローラやビューリゾルバなどを定義し、通常はデータソース定義、中間層サービスなどが含まれています)。

ContextLoaderListenerは、its documentationで説明されているように、Springのルートアプリケーションコンテキストを起動およびシャットダウンするために使用されます(サーブレットコンテキストは継承する)。

中間層にSpringを使用したいが、Spring MVCをプレゼンテーション層として使用したくない場合にも便利です。この場合、ContextLoaderListenerを使用してルートアプリケーションコンテキストのみを定義します。

+0

春のドキュメンテーションリンクでは何も答えていません。また、私は既にWebとアプリケーションのコンテキストを分けていると言います。 –

+6

最初の質問は:ContextLoaderListenerの目的は何ですか?答えは、ルートアプリケーションのコンテキストを起動してシャットダウンすることです。あなたの2番目の質問です:私はコンテキストファイルを管理する最良の方法は混乱しています。答えは、中間層サービスはContextLoaderListenerによってロードされたルートコンテキストに存在する必要があります。 Spring MVC固有のBeanは、ルートから継承するディスパッチャサーブレットのコンテキストで定義する必要があります。 2つのXMLファイルがありますが、単一のコンテキストがあります。ルートコンテキストとサーブレットスコープのコンテキストが必要です。 –

+0

ありがとう! OK、私はルートコンテキストを使用してリスナーを持っている <コンテキスト-param>の ​​contextConfigLocation /WEB-INF/config/applicationContext.xml /WEB-INF/config/spring-mail.xml ルートコンテキストを初期化していませんか? ディスパッチャサーブレットはWebコンテキストを初期化しませんか? Dispatcher Servletにアプリケーションコンテキスト情報を含めないと、トランザクションマネージャがなぜ機能しなくなるのですか? –

関連する問題