Download the full version !!
Here is a better solution provided by feiing. I have some modifications:
Java code
- <Beans>
- <! -- Transaction ManagerForA 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 = "transactionmanager" ref = "transactionmanager"/>
- <Property name = "transactionattributes">
- <Props>
- <Prop key = "*"> propagation_required </prop>
- <Prop key = "find *"> propagation_required, readonly </prop>
- </Props>
- </Property>
- </Bean>
- <BeanClass= "Org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
- <Property name = "beannames">
- <Value> * service, * manager </value>
- </Property>
- <Property name = "interceptornames">
- <List>
- <Value> transactioninterceptor </value>
- <! --
- A new interceptor is added here.
- -->
- <! -- 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>
- <BeanClass= "Org. springframework. transaction. Interceptor. transactionattributesourceadvisor">
- <Property name = "transactioninterceptor" ref = "transactioninterceptor"/>
- </Bean>
- <Bean id = "usermanager"Class= "Some. Package. usermanagerimpl" autowire = "byname"/>
- </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
- <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>