Horse Soldier spring-Declarative Transaction Management-xml

Source: Internet
Author: User
Tags log log

1.com.cy.model User.java and Log.java entities are the same as in the previous section;

2.DAO implementation class Com.cy.dao.impl in the same Userdaoimpl, Logdaoimpl.java and the previous section;

3.userservice.java:

 PackageCom.cy.service;ImportJavax.annotation.Resource;Importorg.springframework.stereotype.Component;Importorg.springframework.transaction.annotation.Transactional;ImportCom.cy.dao.LogDAO;ImportCom.cy.dao.UserDAO;ImportCom.cy.model.Log;ImportCom.cy.model.User; @Component ("UserService") Public classUserService {@ResourcePrivateUserdao Userdao; @ResourcePrivateLogdao Logdao;  Public voidinit () {System.out.println ("Init"); }         PublicUser GetUser (intID) {return NULL; }        /*** Record a log while inserting a user*/     Public voidAdd (user user) {userdao.save (user); Log log=NewLog (); Log.setmsg ("A user saved!");    Logdao.save (log); }         PublicUserdao Getuserdao () {returnUserdao; }         Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; }         Public voiddestroy () {System.out.println ("Destroy"); }}
View Code

Configuration of transaction management in 4.beans.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"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-2.5. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-2.5.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sp Ring-tx-2.5.xsd ">               <Context:annotation-config/>    <Context:component-scanBase-package= "Com.cy"/>        <!--if the following placeholder is used, please look under locations -    <Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        < Propertyname= "Locations">            <value>Classpath:jdbc.properties</value>        </ Property>    </Bean>    <BeanID= "DataSource"Destroy-method= "Close"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "${jdbc.driverclassname}"/>        < Propertyname= "url"value= "${jdbc.url}"/>        < Propertyname= "username"value= "${jdbc.username}"/>        < Propertyname= "Password"value= "${jdbc.password}"/>    </Bean>         <!--Initialize Sessionfactory -    <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource" />         < property name= "Packagestoscan">             <List>                <value>Com.cy.model</value>            </List>        </ Property>        < Propertyname= "Hibernateproperties">            <Props>                <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</prop>                <propKey= "Hibernate.show_sql">True</prop>            </Props>        </ Property>    </Bean>        <BeanID= "Txmanager"class= "Org.springframework.orm.hibernate3.HibernateTransactionManager">          < Propertyname= "Sessionfactory"ref= "Sessionfactory" />    </Bean>        <Aop:config>        <Aop:pointcutID= "Bussinessservice"expression= "Execution (public * com.cy.service). *.*(..))" />        <Aop:advisorPointcut-ref= "Bussinessservice"Advice-ref= "Txadvice" />    </Aop:config>        <Tx:adviceID= "Txadvice"Transaction-manager= "Txmanager">        <tx:attributes>            <Tx:methodname= "GetUser"read-only= "true" />            <Tx:methodname= "add*"Propagation= "REQUIRED"/>        </tx:attributes>    </Tx:advice>    </Beans>

5. Test the code:

 PackageCom.cy.service;Importorg.junit.Test;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.cy.model.User; Public classuserservicetest {@Test Public voidTestadd ()throwsException {classpathxmlapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); UserService Service= (UserService) ctx.getbean ("UserService");        System.out.println (Service.getclass ()); Service.add (NewUser ());    Ctx.destroy (); }}
View Code

1) Normal test, no exception thrown, Ok,user and log are inserted;

2) An exception was thrown manually in the Save method in Logdaoimpl and the insert failed; The user did not insert successfully and has rolled back.

Horse Soldier spring-Declarative Transaction Management-xml

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.