Several other folks already mentioned AOP and AspectJ. If you're not interested in using AspectJ, but still want some AOP functionality, Spring can help you implement it.
To make this work well, you should be coding against interfaces, so Spring can easily use Java Proxies to inject the AOP behavior. What happens is you get an alternate implementation of the interface, one that delegates calls to the "real" implementation, and adds a little extra work before and/or after the method call.
Frankly, you can do this without Proxies or Spring by just hand-writing a new implementation, e.g.
public void foo() {
callbackFunction();
this.delegate.foo();
}
where this.delegate is the "real" impl, but your client ends up calling the foo()
method described above.
...
IFooBar y = new FooBar(); // implements IFooBar
IFooBar x = new FooBarProxy(y); // also implements IFooBar, but with the callback and delegate
x.foo(); // calls y.foo() implicitly
...