Spring @Transactional annotation not working with auto-wiring?

前端 未结 3 1716
闹比i
闹比i 2021-01-05 12:39

My application uses Spring auto-wiring to configure the beans. I have just tried adding @Transactional and the expected proxies do not seem to be called. I want the Personal

相关标签:
3条回答
  • 2021-01-05 13:04

    Define an interface for UpdatePublicMapService, and use that interface under the @Autowired annotation in your controller. Leave the @Transactionnal annotation in the UpdatePublicMapService class implementation, and Spring should deal just fine with your autowired transactionnal object.

    0 讨论(0)
  • 2021-01-05 13:17

    I worked it out. Spring loads two application contexts and the way it loaded resulted in two UpdatePublicMapServices being loaded.

    The ContextLoaderListener loads a root application context. In my case, it found and loaded the UpdatePublicMapService with the transaction wrapper.

    Then the DispatcherServlet loads and reads <name>-servlet.xml. In my case, it used to contain <context:component-scan base-package="com.th"/> which scanned for and loaded UpdatePublicMapService again - but without the transaction wrapper.

    The fix was to trim the component scan configuration to only hit the things I wanted loaded in the servlet and not the root application context.

    0 讨论(0)
  • 2021-01-05 13:17

    I had similar problem but another solution. In my code, there was BeanFactoryHelper.getBean(...) used. If you use this class, you need to call method use() before and release() after.

    0 讨论(0)
提交回复
热议问题