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

编程入门 行业动态 更新时间:2024-10-09 22:23:06
本文介绍了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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:jee="www.springframework/schema/jee" xmlns:context="www.springframework/schema/context" xmlns:tx="www.springframework/schema/tx" xmlns:aop="www.springframework/schema/aop" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/jee www.springframework/schema/jee/spring-jee-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-3.0.xsd www.springframework/schema/tx www.springframework/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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xsi:schemaLocation="www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/context www.springframework/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.xxxmon.auditor.RequestInterceptor"/> <!-- <bean class="com.xxxmon.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 应用程序上下文/根应用程序上下文和事务管理器设置

本文发布于:2023-11-27 12:41:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   应用程序   管理器   事务   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!