spring,hibernate事務(wù)無效問題(由use-default-filters導致的事故)

來張圖


圖片發(fā)自簡書App

前置

最近在開發(fā)一個web項目薪韩,集成hibernate值戳,且使用了OpenSessionInViewFilter劫流,發(fā)現(xiàn)spring的事務(wù)無效了庇楞。進行了如下配置:

spring上下文的applicationContext.xml

<context:component-scan base-package="com.github.mypost">   
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
    <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 事務(wù)特性  -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="insert*" propagation="REQUIRED"/>
        <tx:method name="create*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="remove*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="modify*" propagation="REQUIRED"/>
        <tx:method name="edit*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true" />
    </tx:attributes>
</tx:advice>

<aop:config proxy-target-class="true">
    <aop:pointcut id="serviceMethod" expression="execution(* com.github.mypost.*.service..*+.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>

<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- annotation trasaction -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

springmvc的上下文配置webApplicationContext.xml

<context:component-scan base-package="com.github.mypost">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!--默認的注解映射的支持 -->
<mvc:annotation-driven conversion-service="conversionService"/>

<!-- 攔截器 -->
<mvc:interceptors>
    <bean class="com.github.mypost.commons.web.interceptor.BaseInterceptor"></bean>
</mvc:interceptors>

<!--視圖解釋器,根據(jù)不同的請求展示不同的View -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="viewResolvers">
        <list>
            <!--視圖解釋類 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/view/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </list>
    </property>
</bean>

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />

<!-- 配置文件上傳解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<mvc:view-controller path="/" view-name="index"/>

一直以為是使用了OpenSessionInViewFilter這個類庫的原因?qū)е碌氖。詈蟛虐l(fā)現(xiàn)是由于context:component-scan的use-default-filters導致的問題茴丰。

分析

  1. <context:component-scan>會交給org.springframework.context.config.ContextNamespaceHandler處理达皿;
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
  1. <context:component-scan>的use-default-filters默認true天吓,會交給ClassPathScanningCandidateComponentProvider進行掃描贿肩,看代碼:
protected void registerDefaultFilters() {
    this.includeFilters.add(new AnnotationTypeFilter(Component.class));
    ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
    try {
        this.includeFilters.add(new AnnotationTypeFilter(
                ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
        logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
    }
    catch (ClassNotFoundException ex) {
        // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
    }
    try {
        this.includeFilters.add(new AnnotationTypeFilter(
                ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
        logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
    }
    catch (ClassNotFoundException ex) {
        // JSR-330 API not available - simply skip.
    }
}

可以看到默認ClassPathBeanDefinitionScanner會自動注冊對@Component峦椰、@ManagedBean、@Named注解的Bean進行掃描汰规。如果細心汤功,到此就找到問題根源了。

  1. 對exclude-filter溜哮,include-filter進行過濾
    首先通過exclude-filter 進行黑名單過濾滔金;
    然后通過include-filter 進行白名單過濾;
    否則默認排除茂嗓。

結(jié)論

<context:component-scan base-package="com.github.mypost" use-default-filters="false"> 
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
     <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

那么這段代碼不僅僅對@Controller的注解Bean進行了掃描餐茵,而且對@Compoent注解也掃描了,子注解@Service述吸,@Repository也進行了掃描忿族,所以原來mvc的上下文的Bean被后面的bean替換了。所以如下解決方法:

  1. 如果不需要默認的蝌矛,則use-default-filters=“false”禁用掉道批。
  2. 直接寫明具體Controller包名稱。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末入撒,一起剝皮案震驚了整個濱河市隆豹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌茅逮,老刑警劉巖璃赡,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異献雅,居然都是意外死亡碉考,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門惩琉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來豆励,“玉大人,你說我怎么就攤上這事瞒渠×颊簦” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵伍玖,是天一觀的道長嫩痰。 經(jīng)常有香客問我,道長窍箍,這世上最難降的妖魔是什么串纺? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任丽旅,我火速辦了婚禮,結(jié)果婚禮上纺棺,老公的妹妹穿的比我還像新娘榄笙。我一直安慰自己,他們只是感情好祷蝌,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布茅撞。 她就那樣靜靜地躺著,像睡著了一般巨朦。 火紅的嫁衣襯著肌膚如雪米丘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天糊啡,我揣著相機與錄音拄查,去河邊找鬼。 笑死棚蓄,一個胖子當著我的面吹牛堕扶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播癣疟,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼挣柬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了睛挚?” 一聲冷哼從身側(cè)響起邪蛔,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扎狱,沒想到半個月后侧到,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡淤击,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年匠抗,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片污抬。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡汞贸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出印机,到底是詐尸還是另有隱情矢腻,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布射赛,位于F島的核電站多柑,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏楣责。R本人自食惡果不足惜竣灌,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一聂沙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧初嘹,春花似錦及汉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兔毒。三九已至漫贞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間育叁,已是汗流浹背迅脐。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留豪嗽,地道東北人谴蔑。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像龟梦,于是被迫代替她去往敵國和親隐锭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容