Use beannameautoproxycreator to automatically create a transaction proxy

Source: Internet
Author: User
  • From: http://dev.firnow.com/course/3_program/java/javajs/20100710/387337.html

    Use beannameautoproxycreator to automatically create a transaction proxy
    The following describes an excellent transaction proxy configuration policy: This Configuration Policy can completely avoid incremental configuration, and all transaction proxies are automatically created by the system. The target bean in the container automatically disappears, avoiding the need to use nested beans to ensure that the target bean cannot be accessed.
    This configuration method depends on the bean post-processor provided by spring. This post-processor is used to automatically create a proxy for each bean. The proxy here can be both a transaction proxy and any proxy, you only need an appropriate interceptor.

    The following is the configuration file for configuring the transaction proxy using beannameautoproxycreator:<? XML version = "1.0" encoding = "gb2312"?> <Br/> <! -- The file header of the spring configuration file, including DTD and other information --> <br/> <! Doctype beans public "-// spring // DTD bean // en" <br/> "[url] http://www.springframework.org/dtd/spring-beans.dtd#/url]"> <br/> <beans> <br/> <! -- Define a data source --> <br/> <bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource"> <br/> <! -- Define a database driver --> <br/> <property name = "driverclassname"> <value> COM. mySQL. JDBC. driver </value> </property> <br/> <! -- Define Database URL --> <br/> <property name = "url"> <value> JDBC: mysql: // localhost: 3306/spring </value> </property> <br/> <! -- Define database username --> <br/> <property name = "username"> <value> root </value> </property> <br/> <! -- Define Database Password --> <br/> <property name = "password"> <value> 32147 </value> </property> <br/> </bean> <br /> <! -- Define a sessionfactory of hibernate --> <br/> <bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean"> <br/> <! -- Defines that the sessionfactory must be injected with datasource --> <br/> <property name = "datasource"> <ref local = "datasource"/> </property> <br/> <property name = "mappingresources"> <br/> <list> <br/> <! -- The following is used to list all po ing files --> <br/> <value> person. HBM. XML </value> <br/> </List> <br/> </property> <br/> <property name = "hibernateproperties"> <br/> <props> <br/> <! -- The attribute of sessionfactory of Hibernate is defined here: <br/> for different database connections, select create, update at startup, create-drop --> <br/> <prop key = "hibernate. dialect "> Org. hibernate. dialect. mysqldialect </prop> <br/> <prop key = "hibernate. hbm2ddl. auto "> Update </prop> <br/> </props> <br/> </property> <br/> </bean> <br/> <! -- Define the Transaction Manager and use the Transaction Manager for hibernte --> <br/> <bean id = "transactionmanager" <br/> class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <br/> <! -- Hibernatetransactionmanager Bean must be referenced by injecting a sessionfactory bean. --> <br/> <property name = "sessionfactory"> <ref local = "sessionfactory"/> </property> <br /> </bean> <br/> <! -- Configure the transaction interceptor --> <br/> <bean id = "transactioninterceptor" <br/> class = "org. springframework. transaction. interceptor. transactioninterceptor "> <br/> <! -- The transaction interceptor bean needs to inject a Transaction Manager dependency --> <br/> <property name = "transactionmanager" ref = "transactionmanager"/> <br/> <property name = "transactionattributes "> <br/> <! -- The following defines the transaction propagation Attributes --> <br/> <props> <br/> <prop key = "insert *"> propagation_required </prop> <br/> <prop key = "find *"> propagation_required, readonly </prop> <br/> <prop key = "*"> propagation_required </prop> <br/> </props> <br/> </property> <br /> </bean> <br/>
    <! -- Defines beannameautoproxycreator. This bean is a bean post-processor and does not need to be referenced. Therefore, there is no ID attribute.
    This bean post-processor automatically creates a transaction proxy for the target bean Based on the transaction interceptor<Bean class = "org. springframework. AOP. framework. autoproxy. beannameautoproxycreator "> <br/> specifies which bean names are used to automatically generate a Service proxy --> <br/> <property name =" beannames "> <br/> <! -- The following are all the beans that require automatic transaction proxy Creation --> <br/> <list> <br/> <value> persondao </value> <br/> </List> <br/> <! -- You can add other beans that require automatic transaction proxy creation. --> <br/> </property> <br/> <! -- The following defines the transaction interceptor required by beannameautoproxycreator --> <br/> <property name = "interceptornames"> <br/> <list> <br/> <value> transactioninterceptor </value> <br/> <! -- Other new interceptor can be added here --> <br/> </List> <br/> </property> <br/> </bean> <br/> <! -- Defines Dao bean, because beannameautoproxycreator automatically generates a transaction proxy --> <br/> <bean id = "persondao" class = "Lee. persondaohibernate "> <br/> <property name =" sessionfactory "> <ref local =" sessionfactory "/> </property> <br/> </bean> <br/> </beans> <br/>
    Transcationinterceptor is a transaction interceptor bean that needs to be referenced by a transactionmanager. Spring dependency injection is used in the configuration. The transaction attribute of the transaction interceptor is specified through transactionattributes. This attribute has a props sub-element. The configuration file defines three transaction Propagation Rules:
    All the Methods Starting with insert adopt the transaction Propagation Rules of propagation_required. When the program throws a myexception exception and its sub-exception, the transaction is automatically rolled back. All methods starting with find adopt propagation_required transaction Propagation Rules and are read-only. For other methods, use the transaction propagation rule of propagation_required.
    Beannameautoproxycreator is a proxy builder that generates an automatic proxy Based on the bean name. This bean usually requires two parameters. The first is the beannames attribute, which is used to set which beans need to generate a proxy automatically. Another attribute is interceptornames, which specifies the transaction Interceptor. When a transaction proxy is automatically created, the system generates the corresponding transaction proxy Based on the attributes of these transaction interceptors.
    In order for readers to have information about this configuration method, the SAVE method of persondaohibernate is simply modified. The modified save method is as follows:/** <Br/> * save person instance <br/> * @ Param person the person instance to be saved <br/> */<br/> Public void save (person) {<br/> gethibernatetemplate (). save (person); <br/> // The following two lines of code have no practical significance, just to cause a database exception <br/> datasource DS = NULL; <br/> performanceutils. getconnection (DS); <br/>}< br/>
    Call the Save method in the main program. The main program calls the Save method in the following snippet:
    For (INT I = 0; I <10; I ++) {<br/> // Save the person instance <br/> pdao. save (new person (string. valueof (I), I + 10); <br/>}
    After this segment of the main program is executed, no records are inserted into the data table. If the beannameautoproxycreator configuration is modified to the following format:
    <! -- Defines beannameautoproxycreator. This bean is a bean post-processor and does not need to be referenced. Therefore, there is no ID attribute.
    This bean post-processor automatically creates a transaction proxy for the target bean Based on the transaction interceptor<Bean class = "org. springframework. AOP. framework. autoproxy. beannameautoproxycreator "> <br/> specifies which bean names are used to automatically generate a Service proxy --> <br/> <property name =" beannames "> <br/> <! -- The following are all the beans that require automatic transaction proxy Creation --> <br/> <list> <br/> <! -- Value> persondao </value --> <br/> </List> <br/> <! -- You can add other beans that require automatic transaction proxy creation. --> <br/> </property> <br/> <! -- The following defines the transaction interceptor required by beannameautoproxycreator --> <br/> <property name = "interceptornames"> <br/> <list> <br/> <value> transactioninterceptor </value> <br/> <! -- Other new interceptor can be added here --> <br/> </List> <br/> </property> <br/> </bean> <br/>
    Note the changes to the beannames attribute in the configuration text. Comment out all persondao items, that is, no longer generating transaction proxies for the bean. Execute the main program again. Although the program throws a database exception, the data records are still inserted into the database.
    The two results are compared, which is the role of the transaction proxy.
    This configuration method is quite concise. Every time a new bean is added, if the bean method is required to be transactional, you only need to add a line under the beannames attribute of beannameautoproxycreator, this row tells the bean which bean generates a transaction proxy for post-processing.

  • Beannameautoproxycreator uses JDK dynamic proxy by default. If you want to use the cglib proxy class, add the property Configuration:

    <Property name = "proxytargetclass" value = "true"/>
    The default value of this attribute is false.

  • 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.