Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service。所以应在spring mvc配置文件中,不包括service的扫描。

spring mvc:

  1. <context:component-scan base-package="com.pamirs.pradar.controller">
  2. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  3. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
  4. </context:component-scan>

spring

  1. <context:component-scan base-package="com.pamirs.pradar.controller">
  2. <context:exclude-filter type="annotation"
  3. expression="org.springframework.stereotype.Controller" />
  4. </context:component-scan>

添加@transactional主要注意的问题:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.

这句话的意思是,<tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解,如果你把它放在Dispatcher的应用上下文中,它只检查控制器上的@Transactional注解,而不是你services上的@Transactional注解。

于是,我将事务配置定义在Spring MVC的应用上下文(*-servlet.xml)中,将@Transactional注解打在Controller上,终于事务起作用了。

关于@service、@controller和@transactional 在spring中的位置说明的更多相关文章

  1. Spring 中任意位置获取 session 和 request

    在web.xml中添加监听: <listener> <listener-class>org.springframework.web.context.ContextLoaderL ...

  2. (转)Spring中Singleton模式的线程安全

    不知道哪里的文章,总结性还是比较好的.但是代码凌乱,有的还没有图.如果找到原文了可以进行替换! spring中的单例 spring中管理的bean实例默认情况下是单例的[sigleton类型],就还有 ...

  3. Rhythmk 一步一步学 JAVA (14) Spring-3 @Autowired,@Qualifier @Required @Resource @Component,@Service,@Controller,@Repository @PostConstruct,@PreDestroy

    1.@Autowired 注解:首先在使用时候需要引入配置: <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> ...

  4. 1.2(Spring学习笔记)Spring中的Bean

    一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...

  5. 代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)

    package com.flong.codegenerator; import java.sql.Connection; import java.sql.DatabaseMetaData; impor ...

  6. SprinfJdbcTemplate+SpringMVC 代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)

    代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件) 原文地址: http://jilongliang.iteye.com/blog/2262070 p ...

  7. 二、spring中装配bean

    在spring框架中提供了三种 bean的装配方式,当然这三种装配方式是可以灵活的进行组合使用的,项目中使用最多的是自动装配bean的方式,也就是通过注解的方式进行bean的装配,一下是四种装配方式的 ...

  8. 使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)

    简述: 结合Spring和Hibernate进行开发 使用@Autowired实现依赖注入, 实现一个学生注册的功能,做一个技术原型 从DAO(Repository) -> Service -& ...

  9. 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)

    在使用SpringMvc的时候,配置文件中我们经常看到 annotation-driven 这样的注解,其含义就是支持注解,一般根据前缀 tx.mvc 等也能很直白的理解出来分别的作用.<tx: ...

随机推荐

  1. http代理和SOCKS代理的区别

    HTTP 代理按匿名功能分类(是否具有隐藏 IP 的功能) 非匿名代理:不具有匿名功能. 匿名代理.使用此种代理时,虽然被访问的网站不能知道你的 IP 地址,但仍然可 以知道你在使用代理,有些侦测 I ...

  2. Centos 6 can't found command subscription-manager

    [root@localhost ~]# subscription-manager: command not found-bash: -bash:: command not found resoluti ...

  3. kvm安装及简单使用

    1 cat /etc/redhat-release      CentOS release 6.4 (Final)2 egrep ‘vmx|svm’ /proc/cpuinfo3 yum -y ins ...

  4. centos7 设置 防火墙 开机自启

    CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables. 1.设置firewall开机启动 systemctl enable firewalld 2.禁止firew ...

  5. Flask-SQLAlchemy操作

    Flask-SQLAlchemy   SQLAlchemy 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言 ...

  6. java工厂模式的测试

    /** * 工厂模式的测试 * <p> * 工厂方法模式 * 工厂方法模式是简单工厂模式的进一步抽象化和推广,工厂方法模式里不再只由一个工厂类决定那一个产品类应当被实例化,这个决定被交给抽 ...

  7. Android中利用jsoup解析html页面

    学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...

  8. ASP.NET MVC 3 Razor 多国语言参考解决方案

    http://www.cnblogs.com/think8848/archive/2011/03/20/1989376.html

  9. Onvif协议及其在Android下的实现

    好久没有写博客,今天将前段时间做的Onvif协议在Android上的实现分享给大家. 首先,我们先来了解一下什么是Onvif协议:ONVIF 协议是由Open Network Video Interf ...

  10. realsense data

    line: (43,350) (558,350) (-0.448628,-0.554531,0.995),(0,0,0) 0.713282 ------------------------------ ...