Spring TX: advice and AOP: config configuration transactions

Source: Internet
Author: User
Keyword: Spring configuration transaction

XML Code
  1. <?XML Version="1.0" Encoding=UTF-8"?>
  2. <Beans Xmlns=Http://www.springframework.org/schema/beans"
  3. Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance"
  4. Xmlns: AOP=Http://www.springframework.org/schema/aop"
  5. Xmlns: TX=Http://www.springframework.org/schema/tx"
  6. Xmlns: Context=Http://www.springframework.org/schema/context"
  7. Xsi: schemalocation= "Http://www.springframework.org/schema/beans
  8. Http://www.springframework.org/schema/beans/spring-beans.xsd
  9. Http://www.springframework.org/schema/tx
  10. Http://www.springframework.org/schema/tx/spring-tx.xsd
  11. Http://www.springframework.org/schema/aop
  12. Http://www.springframework.org/schema/aop/spring-aop.xsd"
  13. >
  14. <Bean ID="Transactionmanager"
  15. Class="Org. springframework. Orm. hibernate3.hibernatetransactionmanager"
  16. Abstract="False" Lazy-init="Default" Autowire="Default"
  17. Dependency-Check="Default">
  18. <Property Name="Sessionfactory">
  19. <Ref Bean="Sessionfactory" />
  20. </Property>
  21. </Bean>
  22. <TX: Advice ID="Txadvice" Transaction-Manager="Transactionmanager">
  23. <TX: Attributes>
  24. <TX: Method Name="Add *" Propagation="Required" />
  25. <TX: Method Name="Delete *" Propagation="Required" />
  26. <TX: Method Name="Update *" Propagation="Required" />
  27. <TX: Method Name="Add *" Propagation="Required" />
  28. <! -- <TX: method name = "*" propagation = "true"/> -->
  29. </TX: Attributes>
  30. </TX: Advice>
  31. <AOP: config>
  32. <AOP: pointcut ID="Allmanagermethod"
  33. Expression="Execution (* COM. Service .*.*(..))" />
  34. <AOP: Advisor Advice-ref="Txadvice"
  35. Pointcut-ref="Allmanagermethod" />
  36. </AOP: config>
  37. </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans. Xsdhttp: // response http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id =" transactionmanager "class =" org. springframework. orm. hibernate3.hibernatetransactionmanager "abstract =" false "lazy-init =" default "autowire =" default "dependency-check =" default "> <property name =" s Essionfactory "> <ref bean =" sessionfactory "/> </property> </bean> <TX: Advice id =" txadvice "transaction-Manager =" transactionmanager "> <TX: attributes> <TX: method name = "add *" propagation = "required"/> <TX: method name = "delete *" propagation = "required"/> <TX: method Name = "Update *" propagation = "required"/> <TX: method name = "add *" propagation = "required"/> <! -- <TX: method name = "*" propagation = "true"/> --> </TX: Attributes> </TX: Advice> <AOP: config> <AOP: pointcut id = "allmanagermethod" expression = "execution (* COM. service. *. *(..)) "/> <AOP: Advisor advice-ref =" txadvice "pointcut-ref =" allmanagermethod "/> </AOP: config> </beans>

Eclipse does not recognize the <TX: advice/> label

During spring development, the <TX: advice/> label cannot be recognized by eclipse.

The following error is prompted:

The prefix "TX" for element "TX: advice" is not bound

The cause of this error is as follows:

When defining declarative AOP .. No schema is loaded.

The specific performance is as follows: XML Code

  1. <Beans>
  2. <TX: Advice ID="Txadvice" Transaction-Manager="Transactionmanager">
  3. <TX: Attributes>
  4. <TX: Method Name="Get *" Read-Only="True"/>
  5. <TX: Method Name="*" Propagation="Requires_new" Rollback-="Exception"/>
  6. </TX: Attributes>
  7. </TX: Advice>
  8. <! -- AOP proxy settings -->
  9. <AOP: config Proxy-target-class="True">
  10. ....
  11. </AOP: config>
  12. </Beans>
<Beans> <TX: Advice id = "txadvice" transaction-Manager = "transactionmanager"> <TX: Attributes> <TX: method Name = "get *" Read-Only = "true"/> <TX: method Name = "*" propagation = "requires_new" rollback-for = "exception"/> </TX: Attributes> </TX: Advice> <! -- AOP proxy settings --> <AOP: config proxy-target-class = "true">... </AOP: config> </beans>

In this case, an exception is thrown and the <TX> label is not recognized .. At first, I thought it was not to load the jar package ..

Later, I read the AOP document and found that the "xmlns: AOP" Naming statement should be added to <beans> and the schema address configured by AOP should be specified in "xsi: schemalocation ".

The configuration file is as follows: XML Code

  1. <?XML Version="1.0" Encoding=UTF-8"?>
  2. <Beans Xmlns=Http://www.springframework.org/schema/beans"
  3. Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance"
  4. Xmlns: AOP=Http://www.springframework.org/schema/aop"
  5. Xmlns: TX=Http://www.springframework.org/schema/tx"
  6. Xsi: schemalocation= "Http://www.springframework.org/schema/beans
  7. Http://www.springframework.org/schema/beans/spring-beans.xsd
  8. Http://www.springframework.org/schema/tx
  9. Http://www.springframework.org/schema/tx/spring-tx.xsd
  10. Http://www.springframework.org/schema/aop
  11. Http://www.springframework.org/schema/aop/spring-aop.xsd">
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

These are the key points .. The subsequent configurations remain unchanged ....

Spring uses <TX: Advice> and <AOP: config> to configure transactions. For more information about how to configure transactions, see the spring documentation.

I will explain the meanings of several wildcards in (* COM. Evan. CRM. Service:

| First * -- wildcard: Any return value type |
| Second * -- wildcard package any class under com. Evan. CRM. Service |
| Third * -- any method of any class under com. Evan. CRM. Service |
| The fourth. -- wildcard method can have 0 or more parameters |

To sum up, any class under com. Evan. CRM. Service has any return value type, any number of parameters, and any name.
<TX: advice/> Settings

This section uses the <TX: advice/> label to specify different transaction settings. The default <TX: advice/> Settings are as follows:

The transaction propagation setting is required.

The isolation level is default.

Transaction is read/write

Transaction timeout is dependent on the transaction system by default, or the transaction timeout is not supported.

Any runtimeexception will trigger transaction rollback, but any checked exception will not trigger transaction rollback

These default settings can also be changed. The <TX: method/> attribute settings in the <TX: advice/> and <TX: Attributes/> labels are summarized as follows:
Table 9.1. <TX: method/> Settings

Attribute Required? Default Value Description
Name Yes The method name associated with the transaction property. Wildcard character (*) can be used to specify a batch of methods associated with the same transaction attribute. For example, 'Get * ', 'handle *', and 'on * event.
Propagation No Required Transaction Propagation Behavior
Isolation No Default Transaction isolation level
Timeout No -1 Transaction timeout time (in seconds)
Read-Only No False Is the transaction read-only?
Rollback- No Exception (s) that is triggered for rollback; separated by commas. For example, 'com. Foo. mybusinessexception, servletexception'
No-rollback- No Exception (s) that is not triggered for rollback; separated by commas. For example, 'com. Foo. mybusinessexception

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.