Spring的事务管理难点剖析(2):应用分层的迷惑(二)

2014-11-24 08:46:59 · 作者: · 浏览: 4
ava.sun.com/xml/ns/javaee/web-app_2_5.xsd">

06

07 contextConfigLocation

08 classpath:com/baobaotao/mixlayer/applicationContext.xml

09

10

11 log4jConfigLocation

12 /WEB-INF/classes/log4j.properties

13

14

15

16 org.springframework.web.util.Log4jConfigListener

17

18

19 org.springframework.web.context.ContextLoaderListener

20

21

22

23 user

24 org.springframework.web.servlet.DispatcherServlet

25

26 contextConfigLocation

27 classpath:com/baobaotao/mixlayer/applicationContext.xml

28

29 1

30

31

32 user

33 *.do

34

35

这个配置文件很简单,唯一需要注意的是DispatcherServlet的配置。默认情况下Spring MVC根据Servlet的名字查找WEB-INF下的-servlet.xml作为Spring MVC的配置文件,在此,我们通过contextConfigLocation参数显式指定Spring MVC配置文件的确切位置。
将org.springframework.jdbc及org.springframework.transaction的日志级别设置为DEBUG,启 动项目,并访问http://localhost:8088/chapter10/logon.do userName=tom应 用,MixLayerUserService#logon方法将作出响应,查看后台输出日志,如下所示:

引用
Returning cached instance of singleton bean 'transactionManager'
Creating new transaction with name [com.baobaotao.mixlayer.MixLayerUserService.logon]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
(DataSourceTransactionManager.java:204) - Acquired Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver] for JDBC transaction
(DataSourceTransactionManager.java:221) - Switching JDBC Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver] to manual commit
(JdbcTemplate.java:810) - Executing prepared SQL update
(JdbcTemplate.java:569) - Executing prepared SQL statement [UPDATE t_user u SET u.score = u.score + WHERE user_name = ]
(JdbcTemplate.java:819) - SQL update affected 0 rows
(AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
(DataSourceTransactionManager.java:264) - Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver]

日志中红色部分说明了MixLayerUserService#logon方法已经正确运行在事务上下文中。

Spring框架本身不应是代码复杂化的理由,使用Spring的开发者应该是无拘无束的:从实际应用出发,去除那些所谓原则性的接口,去掉强制分层的束缚,简单才是硬道理。