Tune learning in Java

zhaozj2021-02-12  125

Time to learn Hibernate, I read the package of Hibernate in Spring. The HibernateTemplate class used ExecuteTemplate class to actually do various Hibernate operations, where HibernateCallback is an interface for callback public interface hibernateCallback { Object doinhibernate (session session) throws hibernateException, Sqlexception;

Such various operations can be done by implementing all their interfaces HibernateCallback, such as save: public void save (final Object entity, final Serializable id) throws DataAccessException {execute (new HibernateCallback () {public Object doInHibernate (Session session) throws HibernateException {session.save (entity, id); return null;}});

excute function is approximately as follows: public Object execute (HibernateCallback action) throws DataAccessException {....... try {Object result = action.doInHibernate (session); flushIfNecessary (session, existingTransaction); return result;} ..... ....

The callback in Java is roughly defined an interface, and the caller does not need to know the specific implementation of this interface, just call the interface function. In the example above Spring, use Inner Class to transmit the interface to the caller, if not Inner Class, then pass the reference to the caller to the caller. As in Examples think in java: // Callback Interface public interface FilenameFilter {boolean accept (File dir, String name);} // interface class DirFilter implements FilenameFilter {private String afn; public DirFilter (String afn) {this.afn = Afn;} public boolean accept (file dir, string name) {string f = new file (name) .GetName (); returnif.indexof (afN)! = -1;}} // call method public string [ ] List (filenamefilter filter) // actually calls String [] list = file.list (New Dirfilter ("wf"));

This is just the callback in Java, and in the software module call relationship is back, the callback is a two-way call mode, that is, the called party will also call the other party when the interface is called (see HTTP) : //www-900.ibm.com/developerWorks/cn/linux/L-CALLBACK/index.shtml) The example is not established with the example above Java, and the caller is not called when the interface is called. The other party's interface, may this be the difference between the callback mechanism and the callback function? The callback mechanism in Java is also possible. For example, in a multi-threaded program, the sub-thread can call a function in the main thread to send some messages to the main thread. This example is in the 125 page of Java Network Programming 2nd, here only Write a segment: sub-thread: public class callbackdigest IMPLEMENT RUNABLE {... PUBLIC VOID RUN () {... CallbackDigestuserInterface.ReceiveDigest (.) ;. ..} ....} and in the main program: public class callbackdigestuserinterface {... public static void receificest () {........} ...}

Learn more about the callback to see the Command mode:) http://www.jdon.com/designpatterns/command.htmhtp://www.javaworld.com/javaworld/javatips/jw-javatip68.html

At present, I know so much, wrong or missing, please refer to it.

转载请注明原文地址:https://www.9cbs.com/read-7568.html

New Post(0)