Pseudocode for callback demonstration

Source: Internet
Author: User

Package COM. jadyer. callback; </P> <p>/** <br/> * the pseudo <SPAN class = 'wp _ keywordlink '> Code </span> <br/> * of the callback demo the class is equivalent to the application <SPAN class = 'wp _ keywordlink'> Program </span> <br/> */<br/> public class callbackdemo {<br /> Public static void main (string [] ARGs) {<br/> persistmanager = new persistmanager (); <br/> persistmanager. persist (New hibernatecallback () {<br/> Public object doinhibernate (Session sess Ion) throws hibernateexception, sqlexception {<br/> User user = new user (); <br/> User. setusername (""); <br/> User. setpassword ("Dust Removal"); <br/> session. save (User); <br/>}< br/> }); <br/>}</P> <p>/** <br/> * persistence management class <br/> */<br/> class persistmanager {<br/> // The persist(resumable upload is equivalent to gethibernatetemplate(cmd.exe cutefind () method <br/> // they must also receive a hibernatecallback object <br/> Public void persist (hibernatecallback Act Ion) {<br/> session = NULL; <br/> try {<br/> session = hibernateutils. getsession (); <br/> session. begintrasaction (); </P> <p> // when doinhibernate (Session) is executed, pass the session to <br/> // so we can get the session directly in the anonymous class and use the action directly. <br/>. doinhibernate (session); // callback </P> <p> session. gettransaction (). commit (); <br/>}catch (exception e) {<br/> session. gettransaction (). rollback (); <br/>}finally {<br/> hibernateutils. c Losesession (session); <br/>}</P> <p>/** <br/> * The interface defined here, for the moment, make it the same name and method as the hibernatecallback interface of spring <br/> */<br/> interface hibernatecallback {<br/> Object doinhibernate (session) throws hibernateexception, sqlexception; <br/>}</P> <p>/************** [callback function ]********* **************************************** * **********/<br/> // callback function: optimize the code to reduce the template. It can extract some template statements <br/> // when writing JDBC or hibernate, you need to get the session, start and commit the transaction, try {} catch {}, roll back, and so on. <br/> // we used to solve the problem through dynamic proxy, switch it into the code. In fact, you can also use the callback mechanism to solve similar problems <br/>/*************** [Use of callback ]******* **************************************** * ***********/<br/> // by calling the persistence method of the persistence manager, persistmanager is called. for example, <br/> // persist () needs to pass an object that implements the hibernatecallback interface, here we also use the anonymous method <br/> // Since the hibernatecallback interface is implemented, the doinhibernate () interface must be implemented in the anonymous class () method <br/> // because the parameter of doinhibernate () method is session, we can get the session when implementing this method. <br/> // after obtaining the session, then we can do the relevant processing. assume that we have a new user and then use session to save the user, and so on <br/>/************* [callback details ]**************** **************************************** * ***/<br/> // for how to obtain the session and enable the transaction, transaction rollback, commit, and session shutdown are all ignored. <br/> // These template-based codes are all managed in persist (). When we call persistmanager. during the persist () method <br/> // The persistmanager is actually called. java Management class, and then we can do relevant things in the management class <br/> // we can write the template-based code to persistmanager. in Java, <br/> // In the persist () method can reduce the number of template class code. Instead, we only encapsulate the code into persistmanager. <br/> /****************************** **************************************** ****************/

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