0

I am reading the spring documentation. All the time I get this word "callback". For example: Lifecycle callbacks Initialization callbacks etc.

How do we understand callback function ? And when you say "Lifecycle callbacks" in the spring, what does it mean ?

I have kept efforts in understanding this but I am not sure if I have understood correctly. Please help.

Anveshan
  • 614
  • 1
  • 9
  • 21

2 Answers2

3

The wiki has a good explanation:

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

Also check this interesting article Java Tip 10: Implement callback routines in Java

A sample example:

    interface CallBack {
    void methodToCallBack();
}

class CallBackImpl implements CallBack {
    public void methodToCallBack() {
        System.out.println("I've been called back");
    }
}

class Caller {

    public void register(CallBack callback) {
        callback.methodToCallBack();
    }

    public static void main(String[] args) {
        Caller caller = new Caller();
        CallBack callBack = new CallBackImpl();
        caller.register(callBack);
    }
}

Paul Jakubik, Callback Implementations in C++.

Callbacks are most easily described in terms of the telephone system. A function call is analogous to calling someone on a telephone, asking her a question, getting an answer, and hanging up; adding a callback changes the analogy so that after asking her a question, you also give her your name and number so she can call you back with the answer.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
3

LifeCycle

In the context of Spring beans (which I believe is the context of what you are reading - hard to tell with the little info you've provided), beans go through different lifecycle phases (like creation and destruction). Here are the lifecycle phases of the Spring bean you can hook into:

enter image description here

Callback

@R.T.'s wikipedia link to what a callback is, is a good starting point to understanding callbacks. In Java, the concept of callback is implemented differently.

In object-oriented programming languages without function-valued arguments, such as in Java before its 1.7 version, callbacks can be simulated by passing an instance of an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation.

A good example is given by @SotiriosDelamanolis in this answer, which I'll post here just for context.

/**
 * @author @SotiriosDelamanolis 
 * see https://stackoverflow.com/a/19405498/2587435
 */
public class Test {
    public static void main(String[] args) throws  Exception {
        new Test().doWork(new Callback() { // implementing class            
            @Override
            public void call() {
                System.out.println("callback called");
            }
        });
    }

    public void doWork(Callback callback) {
        System.out.println("doing work");
        callback.call();
    }

    public interface Callback {
        void call();
    }
}

LifeCycle Callback

By looking at the image above, you can see that Spring allows you to hook into the bean lifecyle with some interfaces and annotations. For example

Hooking into the bean creation part of the lifecycle, you can implements InitializingBean, which has a callback method afterPropertiesSet(). When you implements this interface, Spring pick up on it, and calls the afterPropertiesSet().

For example

public class SomeBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() {  // this is the callback method
                                        // for the bean creation phase of the 
                                        // spring bean lifecycle
        // do something after the properties are set during bean creation
    }
}

Alternatively, you can use the @PostConstruct method for a non-InitializingBean implemented method, or using the init-method in xml config.

The diagram shows other lifecycle phases you can hook into and provide "callback" method for. The lifecycle phases are underlined at the top in the diagram

You can see more at Spring reference - Lifecycle Callbacks

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720