Web 应用程序上下文/根应用程序上下文和事务管理器设置 [英] Web-application context/ root application context and transaction manager setup

查看:45
本文介绍了Web 应用程序上下文/根应用程序上下文和事务管理器设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题,

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

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

Below are the my entries in web.xml, All MVC beans are defined in servlet-context.xml All database and annotation based transaction management is defined in applicationContext.xml and I'm using container managed transaction in JBoss

如果我将如下突出显示的 applicationContext.xml 传递给 DispatcherServlet,则事务管理器工作正常.但我认为我们应该只将 Spring MVC 上下文信息传递给 DispatcherServlet.

The trasaction manager works fine if I pass the applicationContext.xml as highlighted below as to DispatcherServlet. But I thought we should only pass the Spring MVC context info to 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>

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

Thanks for you help! I know its quote long, but wanted to make myself understood better

推荐答案

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

As explained in the documentation, every dispatcher servlet has its own application context, where you typically define controllers, view resolvers, etc., and which inherits (and can override beans) from a root application context, which typically contains data source definitions, middle tier services, etc.

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

The ContextLoaderListener, as its documentation explains, is used to to start up and shut down Spring's root application context (from which the servlet contexts inherit).

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

It's also useful when you want to use Spring for your middle tier, but you don't want to use Spring MVC as your presentation layer. In this case, you only define a root application context using ContextLoaderListener.

这篇关于Web 应用程序上下文/根应用程序上下文和事务管理器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆