Spring application doesn't appear to be persisting data

后端 未结 6 2105
甜味超标
甜味超标 2021-01-20 13:07

I\'m trying to write something into my database but it\'s not working despite it reporting \"Successfully completed request\". After the success everything seems to work fin

6条回答
  •  渐次进展
    2021-01-20 14:03

    Make sure that you don't have exact duplicate elements in both xml configurations. If you have this you are basically duplicating all your bean instances. What you initially had was all beans get loaded by the ContextLoaderListener and those are proxied due to the existence of .

    Now if you have the same in your payment-servlet.xml this is going to scan again for all beans creating another instance, however due to the fact that there is no it will not be proxied and no transactions applied.

    What now happens is that as soon as you need one of your @Service annotated beans the DispatcherServlet looks first in its own ApplicationContext to see if there is bean to satisfy its needs. If there is it is going to be used (your current case) if there isn't it will consult the parent context (the one loaded by the ContextLoaderListener).

    What you need to do is configure the ContextLoaderListener to scan for everything BUT @Controller annotated beans and the DispatcherServlet to scan ONLY for @Controller annotated beans. This can be done by configuring the correctly.

    applicationContext.xml

    
      
    
    

    payment-servlet.xml

    
      
    
    

    This will give you transactions and only single instances of your beans. You should remove the from the payment-servlet.xml file.

    There is still an open JIRA issue to have this included in the reference guide. Also a thread in the Spring Community Forums explaining this.

提交回复
热议问题