spring mvc使用註解方式;service使用@service註解 事務使用@Transactionaljava
事務配置使用web
在插入或更新數據時,無報錯,但數據庫中無結果,而查詢正常。疑爲事務未提交。spring
方式用來掃描該包以及其子包下的@Controller註解的類,歸入spring管理,而同時spring 容器也須要使用這種方式掃描包含@Service、@Components、@Required、@Autowired等註解用來管理bean和完成DI。數據庫
出如今spring mvc的配置文件中時,web 容器在掃描包含@Service或@Components的類幷包含@Transaction是,此時@Transaction併爲完成,致使事務未被註冊。express
四、問題解決spring-mvc
分兩部分掃描:mvc
spring-mvc.xml中掃描controllerapp
application.xml中掃描其餘的ui
SpringMVC.xml配置文件--> 的只掃描controller組件 注意使用 use-default-filters="false"
<context:component-scan base-package="com.yx.*" use-default-filters="false" >
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
ApplicationContext.xml配置文件-->掃描除controller外的全部組件
<context:component-scan base-package="com.yx.*" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>spa
經調試代碼發現: 一、若是不設置use-default-filters="false",則Spring會掃描並優先註冊默認的bean(固然包括標記爲@Service的bean),這樣,標記爲@Transactional的service因爲transaction manager還沒有註冊而未能生效,致使事務管理失效。 原理是:標記爲@Transactional的service會wrap爲通過transactional proxied(無論是CGLIB based或是JDK based)的bean,而再也不是純的service;