我有两个问题,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>
感谢您的帮助!我知道它的引用很长,但是想要让自己更好理解
没有真正回答任何东西除了春文件的链接,而且什么我已经说,我是分离的网络和应用程序上下文 – 2012-02-10 12:47:46
你的第一个问题是:什么样的ContextLoaderListener的目的。答案是:启动并关闭根应用程序上下文。你的第二个问题是:我很困惑什么是管理上下文文件的最佳方式。答案是:中间层服务应该位于根环境中,由ContextLoaderListener加载。应该在调度器servlet的上下文中定义Spring MVC特定的bean,该上下文从根继承。您有两个XML文件,但只有一个上下文。你应该有一个根上下文和一个servlet范围的上下文。 – 2012-02-10 12:58:18
谢谢! 行,我有根上下文 <的context-param> 的contextConfigLocation听者 /WEB-INF/config/applicationContext.xml /WEB-INF/config/spring-mail.xml PARAM -value> context-param> 是不是初始化不是根上下文? 而且调度程序servlet不会初始化web上下文吗? 此外,如果我不在Dispatcher Servlet中包含应用程序上下文信息,为什么事务管理器停止工作? –
2012-02-10 14:45:21