Scenario:
When using spring mvc, I used an annotation such as @ Service and found that the transaction declared by @ Transactional does not work.
My configuration is as follows:
<Mvc: annotation-driven/>
<Context: component-scan base-package = "org. test"/>
<Bean id = "txManager" class = "org. springframework. jdbc. datasource. cetcetransactionmanager">
<Property name = "dataSource" ref = "dataSource"/>
</Bean>
<Tx: annotation-driven transaction-manager = "txManager"/>
The reason is::
The context of component-scan is different from that of the transaction. The configuration of component-scan is loaded by servlet, And the configuration file of the transaction is loaded by Listener.
My solution:
Follow the following configuration to prevent spring from scanning the @ Service annotation class when the application is started. The servlet configuration file is written as follows:
<Context: component-scan base-package = "org. test">
<Context: exclude-filter type = "annotation" expression = "org. springframework. stereotype. Service"/>
</Context: component-scan>
Added the following content in the listener context:
<Context: component-scan base-package = "org. test"> </context: component-scan>