2012-02-10 42 views
4

我有兩個問題,Web的應用程序上下文/根應用上下文和事務管理器設置

在Spring MVC應用程序,什麼是具有ContextLoaderListener的目的是什麼?

下面是在web.xml中我的條目, 所有MVC豆中的servlet-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> 

servlet的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中所解釋的,每個調度程序servlet都有自己的應用程序上下文,其中您通常定義控制器,視圖解析器等,以及繼承(並且可以覆蓋bean通常包含數據源定義,中間層服務等。

ContextLoaderListener,如its documentation解釋,用於啓動和關閉Spring的根應用程序上下文(從該應用程序上下文中,servlet上下文繼承)。

它也是有用的,當你想使用Spring爲你的中間層,但你不想使用Spring MVC的表示層。在這種情況下,您只使用ContextLoaderListener定義根應用程序上下文。

+0

沒有真正回答任何東西除了春文件的鏈接,而且什麼我已經說,我是分離的網絡和應用程序上下文 – 2012-02-10 12:47:46

+6

你的第一個問題是:什麼樣的ContextLoaderListener的目的。答案是:啓動並關閉根應用程序上下文。你的第二個問題是:我很困惑什麼是管理上下文文件的最佳方式。答案是:中間層服務應該位於根環境中,由ContextLoaderListener加載。應該在調度器servlet的上下文中定義Spring MVC特定的bean,該上下文從根繼承。您有兩個XML文件,但只有一個上下文。你應該有一個根上下文和一個servlet範圍的上下文。 – 2012-02-10 12:58:18

+0

謝謝! 行,我有根上下文 <的context-param> ​​的contextConfigLocation聽者 /WEB-INF/config/applicationContext.xml /WEB-INF/config/spring-mail.xml 是不是初始化不是根上下文? 而且調度程序servlet不會初始化web上下文嗎? 此外,如果我不在Dispatcher Servlet中包含應用程序上下文信息,爲什麼事務管理器停止工作? – 2012-02-10 14:45:21

相關問題