Keyword: Spring configuration transaction
XML
Code
- <?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/beans
- Http://www.springframework.org/schema/beans/spring-beans.xsd
- Http://www.springframework.org/schema/tx
- Http://www.springframework.org/schema/tx/spring-tx.xsd
- Http://www.springframework.org/schema/aop
- 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="Sessionfactory">
- <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>
<? 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
- <Beans>
- <TX: Advice ID="Txadvice" Transaction-Manager="Transactionmanager">
- <TX: Attributes>
- <TX: Method Name="Get *" Read-Only="True"/>
- <TX: Method Name="*" Propagation="Requires_new" Rollback-="Exception"/>
- </TX: Attributes>
- </TX: Advice>
- <! -- AOP proxy settings -->
- <AOP: config Proxy-target-class="True">
- ....
- </AOP: config>
- </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
- <?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/beans
- Http://www.springframework.org/schema/beans/spring-beans.xsd
- Http://www.springframework.org/schema/tx
- Http://www.springframework.org/schema/tx/spring-tx.xsd
- Http://www.springframework.org/schema/aop
- 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 |