- 浏览: 585383 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
a1641693970:
还不错,学习了
BeanUtils使用总结(二)LazyDynaBean -
zjfshowtime:
很好的办法,学习了。
ORA-28001: the password has expired -
ya654277yo:
哦,多谢分享
Apache整合Tomcat后get方式提交中文乱码问题解决 -
foolkeeper:
nice !!
jvm内存参数设定 -
tracy821:
谢谢了,找了好久才找到
关于Spring 声明式事务处理时,throws exception不回滚的问题
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
看一下ContextLoaderListener的源码,这是一个ServletContextListener
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
contextLoader的源码
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws BeansException {
long startTime = System.currentTimeMillis();
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
servletContext.log("Loading Spring root WebApplicationContext");
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
WebApplicationContext wac = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
if (logger.isInfoEnabled()) {
logger.info("Using context class [" + wac.getClass().getName() +
"] for root WebApplicationContext");
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext [" + wac +
"] as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return wac;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
注意WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,这里面放了WebApplicationContext,需要使用时从ServletContext取出
可以使用WebApplicationContextUtils得到WebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (attr == null) {
return null;
}
if (attr instanceof RuntimeException) {
throw (RuntimeException) attr;
}
if (attr instanceof Error) {
throw (Error) attr;
}
if (!(attr instanceof WebApplicationContext)) {
throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
}
return (WebApplicationContext) attr;
}
关键的问题在于struts如何启动的spring的,ContextLoaderPlugIn的源码
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getModulePrefix();
}
不同加载的Key竟然不同,原因就是WebApplicationContext放在那里的问题,可spring调用的时候会根据WebApplicationContext里面定义的那个名字去找的,问题出在这里
在struts-config.xml中配置
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
原理是这样的,Struts虽然只能有一个ActionServlet实例,但是对于不同的子应用分别能有自己的RequestProcessor实例每个RequestProcessor实例分别对应不同的struts配置文件。
子应用的ProcessorClass类必须重写一般就是继承RequestProcessor类,然后再其配置文件的controller元素中的<processorClass>属性中作出修改。那么当
getRequestProcessor(getModuleConfig(request)).process(request,response); 就能根据request选择相应的moduleconfig,再根据其<processorClass>属性选择相应的 RequestProcessor子类来处理相应的请求了。
发表评论
-
Spring 多数据源声明式事务控制(PlatformTransactionManager )
2014-04-11 18:31 4762首先是TransactionManager的配置: ... -
Spring 动态数据源(AbstractRoutingDataSource )
2014-04-11 18:25 1714Spring动态配置多数据源,即在大型应用中对数据进行横向 ... -
(转)Spring Security 3.0 多页面登录配置
2012-10-19 17:01 5470网上很多文章是关于Spri ... -
Spring 中集成 JOTM 配置 JTA 事务
2011-12-27 19:52 1629Spring 中集成 JOTM 配置 JTA 事务: ... -
Spring 3.1整合EhCache
2011-08-29 16:42 1484Spring 3.1提供了对cache的 ... -
spring集成xfire教程
2011-04-02 15:20 1621配置web.xml <!-- SPRING ... -
关于Spring 声明式事务处理时,throws exception不回滚的问题
2010-09-29 23:31 9042前一段时间,项目代码评审,发现有TX不使用Spri ... -
iBatis SqlMap 的配置总结 收藏
2010-07-22 14:30 3794核心提示:SqlMap的配 ... -
ibatis oscache 使用中miss cache
2010-06-10 13:17 2926原始配置信息: <cacheModel id=&quo ... -
ibatis oscache配置以及【文件名、目录名或卷标语法不正确】错误解决
2010-05-26 13:13 3448sqlmap-config.xml 配置如下: < ... -
Hibernate+ehcache二级缓存技术
2010-02-01 20:57 9611、首先设置EhCache,建立配置文件ehcache.XML ... -
Hibernate的缓存讨论
2010-02-01 20:50 1154一般系统中有三种情况 ... -
OpenSessionInView Filter 但是发现不生效(转载)
2010-01-25 15:35 1832今天有一个朋友问了我一个问题,他使用的是Hibernate/S ... -
ApplicationContext和BeanFactory的关于单实例bean装载的区别
2010-01-19 17:04 1271Bean工厂延迟载入所有的Bean,直到getBean()方法 ... -
Spring AOP 学习小结(转载)
2010-01-06 21:24 1477一、AOP 概念 Joinpoint ... -
AOP术语(转载)
2010-01-06 21:04 1075AOP术语(转载) 2009-07-16 22:12 ... -
spring Lookup方法注入(转)---cglib解决子类过多问题、重构
2010-01-06 17:33 5963“Lookup方法”可以使Spri ... -
Spring JTA应用之Atomikos配置
2010-01-05 23:17 3804Atomikos,是一个基于Java的开源事务管理器,提供了事 ... -
多对多关系中Set的查询
2010-01-05 22:27 1138多对多关系中Set的查询 一个老师教多个学生,一个学生 ... -
ibatis 使用 in子句
2009-09-28 18:44 1912因为in的性能问题,平常不怎么用。今天特殊需求,在ibatis ...
相关推荐
org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web.context.ServletContextAware.class org.springframework.web....
3. **配置Spring启动方式**: 在`web.xml`中,你需要配置`ContextLoaderListener`或`ContextLoaderServlet`。例如: - 使用监听器方式: ```xml <listener-class>org.springframework.web.context....
Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...
在Java Web开发中,Spring和Struts是两个非常流行的框架,它们在构建应用程序时有着不同的配置方式。...这两种方式各有优劣,但都体现了Spring的灵活性和可扩展性,可以根据项目需求选择合适的集成方式。
除了`ContextLoaderListener`,Spring还提供了另一种方式来初始化WebApplicationContext,那就是通过`ContextLoaderServlet`。这是一种Servlet,它同样可以在`web.xml`中配置,通常用于处理特定的URL模式,并在启动...
ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener 接口,在 web.xml 文件中配置这个监听器,启动...
可以看出,有两种方法:一个是用 ContextLoaderListener 这个 Listerner,另一个是 ContextLoaderServlet 这个 Servlet,两者都是在 Web 应用启动的时候来初始化 WebApplicationContext。我个人认为 Listerner 要比 ...
- **配置`web.xml`**:在Web应用的部署描述符文件中注册Spring的上下文加载监听器,以确保在启动应用时加载Spring的配置信息。例如: ```xml <servlet-name>contextLoader <servlet-class>org.springframework...
ContextLoaderServlet ContextRefreshedEvent ContextSingletonBeanFactoryLocator ControlFlow ControlFlowFactory ControlFlowFactory.Jdk13ControlFlow ControlFlowFactory.Jdk14ControlFlow ...