Beannameautoproxycreator automatically creates a transaction proxy

Source: Internet
Author: User

 

Download the full version !!
Here is a better solution provided by feiing. I have some modifications:

Java code
  1. <Beans>
  2. <! -- Transaction ManagerForA single hibernate sessionfactory (alternative to JTA); -->
  3. <Bean id = "transactionmanager"Class= "Org. springframework. Orm. hibernate3.hibernatetransactionmanager">
  4. <Property name = "sessionfactory">
  5. <Ref bean = "sessionfactory"/>
  6. </Property>
  7. </Bean>
  8. <Bean id = "transactioninterceptor"Class= "Org. springframework. transaction. Interceptor. transactioninterceptor">
  9. <Property name = "transactionmanager" ref = "transactionmanager"/>
  10. <Property name = "transactionattributes">
  11. <Props>
  12. <Prop key = "*"> propagation_required </prop>
  13. <Prop key = "find *"> propagation_required, readonly </prop>
  14. </Props>
  15. </Property>
  16. </Bean>
  17. <BeanClass= "Org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
  18. <Property name = "beannames">
  19. <Value> * service, * manager </value>
  20. </Property>
  21. <Property name = "interceptornames">
  22. <List>
  23. <Value> transactioninterceptor </value>
  24. <! --
  25. A new interceptor is added here.
  26. -->
  27. <! -- Value> exceptionhandler </value>
    <Value> timehandler </value -->
    </List>
    </Property>
    </Bean>
    <! -- Advice records the information when an exception occurs. -->
    <Bean id = "exceptionhandler" class = "com. unsap. Dec. Platform. util. exceptionhandler"/>
    <Bean id = "exceptionhandlereadvisor" class = "org. springframework. AOP. Support. regexpmethodpointcutadvisor">
    <Property name = "advice">
    <Ref bean = "exceptionhandler"/>
    </Property>
    <! -- Valid for any method of the specified class -->
    <Property name = "patterns">
    <Value>. *. * </value>
    </Property>
    </Bean>
    <! -- Advice records the method record time -->
    <Bean id = "timehandler" class = "com. unsap. Dec. Platform. util. timehandler"/>
    <Bean id = "timehandleradvisor" class = "org. springframework. AOP. Support. regexpmethodpointcutadvisor">
    <Property name = "advice">
    <Ref bean = "timehandler"/>
    </Property>
    <! -- Valid for any method of the specified class -->
    <Property name = "patterns">
    <Value>. *. * </value>
    </Property>
    </Bean>
  28. <BeanClass= "Org. springframework. transaction. Interceptor. transactionattributesourceadvisor">
  29. <Property name = "transactioninterceptor" ref = "transactioninterceptor"/>
  30. </Bean>
  31. <Bean id = "usermanager"Class= "Some. Package. usermanagerimpl" autowire = "byname"/>
  32. </Beans>
<Beans> <! -- Transaction Manager for a single hibernate sessionfactory (alternative to JTA); --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "> <ref bean =" sessionfactory "/> </property> </bean> <bean id =" transactioninterceptor "class =" org. springframework. transaction. interceptor. transactioninterceptor "> <property name =" Tran Sactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <props> <prop key =" * "> propagation_required </prop> <prop key =" find * "> propagation_required, readonly </prop> </props> </property> </bean> <Bean class = "org. springframework. AOP. framework. autoproxy. beannameautoproxycreator "> <property name =" beannames "> <value> * service, * manager </value> </property> <property name =" intercepto Rnames "> <list> <value> transactioninterceptor </value> <! -- New interceptor --> </List> </property> </bean> <Bean class = "org. springframework. transaction. interceptor. transactionattributesourceadvisor "> <property name =" transactioninterceptor "ref =" transactioninterceptor "/> </bean> <bean id =" usermanager "class =" some. package. usermanagerimpl "autowire =" byname "/> </beans>

The next increment is this section: Java code

  1. <Bean id = "usermanager"Class= "Some. Package. usermanagerimpl" autowire = "byname"/>
        <bean id="userManager" class="some.package.UserManagerImpl" autoWire="byName"/>

The method is as simple and intuitive as configuring common beans. You do not need to modify the existing service interfaces.

I changed the feiing transactionattributessource to transactionattributes, and changed defaadadvisorautoproxycreator to beannameautoproxycreator. I think that not every bean in the context needs transactions, but it can be destroyed by performing AOP at the service layer.

Like Robbin, feiing is very desirable because itSeparation of attention in the XML configuration file

Inspired by hongliang, I used this method now, but I found a bug. * Manager is used to match all managerobjects. However, it seems that the configuration will also match transactionmanager, and then define methods other than find to be transactional. That is to say, when transactionmanager executes gettransaction, it will need a transaction, and then an endless loop will cause overflow ..

The solution is to rename transactionmanager as transactionmanagerhibernate. When doing this, you must pay attention to the name issue. Don't be careful. Let's get an irrelevant stuff in...

By the way, there are a lot of * managers when adding acegi, and I plan to think of a better name matching ..

========================================================== ====================================

<Bean id = "transactionadmin" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
<Bean id = "transactioninterceptor" class = "org. springframework. transaction. Interceptor. transactioninterceptor">
<Property name = "transactionmanager" ref = "transactionadmin"/>
<Property name = "transactionattributes">
<Props>
<Prop key = "Save *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "create *"> propagation_required </prop>
<Prop key = "Remove *"> propagation_required </prop>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "query *"> propagation_required,-exception </prop>
<Prop key = "*"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
<Bean class = "org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
<Property name = "proxytargetclass">
<Value> true </value>
</Property>
<Property name = "beannames">
<List>
<Value> * manager * </value>
</List>
</Property>
<Property name = "interceptornames">
<List>
<Value> transactioninterceptor </value>
</List>
</Property>
</Bean>
<Bean id = "exceptionhandler" class = "com. unsap. Dec. Platform. util. exceptionhandler"/>
<Bean id = "timehandler" class = "com. unsap. Dec. Platform. util. timehandler"/>
<Bean class = "org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
<Property name = "proxytargetclass">
<Value> true </value>
</Property>
<Property name = "beannames">
<List>
<Value> * manager * </value>
<Value> * Action * </value>
<Value> * Dao * </value>
</List>
</Property>
<Property name = "interceptornames">
<List>
<Value> exceptionhandler </value>
<Value> timehandler </value>
</List>
</Property>
</Bean>

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.