Web应用程序上下文/根应用程序上下文和事务管理器设置

Tar*_*ngh 4 spring-mvc

我有两个问题,

在Spring MVC应用程序中,拥有ContextLoaderListener的目的是什么?

下面是我在web.xml中的条目,所有MVC bean都在servlet-context.xml中定义.所有基于数据库和注释的事务管理都在applicationContext.xml中定义,我在JBoss中使用容器管理事务

如果我将下面突出显示的applicationContext.xml传递给DispatcherServlet,则trasaction管理器工作正常.但我认为我们应该只将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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!我知道它的引用很长,但想让自己更好地理解

JB *_*zet 13

文档所述,每个调度程序servlet都有自己的应用程序上下文,您通常在其中定义控制器,视图解析程序等,并从根应用程序上下文继承(并可以覆​​盖bean),根应用程序上下文通常包含数据源定义,中间等级服务等

ContextLoaderListener,正如其文档所解释的,用于启动和关闭Spring的根应用程序上下文(servlet上下文从中继承).

当您希望将Spring用作中间层时,它也很有用,但您不希望将Spring MVC用作表示层.在这种情况下,您只使用ContextLoaderListener定义根应用程序上下文.

  • 您的第一个问题是:ContextLoaderListener的目的是什么.答案是:启动并关闭根应用程序上下文.你的第二个问题是:我很困惑什么是管理上下文文件的最佳方式.答案是:中间层服务应该在根上下文中,由ContextLoaderListener加载.Spring MVC特定bean应该在调度程序servlet的上下文中定义,该上下文继承自根目录.您有两个XML文件,但只有一个上下文.您应该有一个根上下文和一个servlet范围的上下文. (6认同)