2

Is it possible to add a hook to a meothd without changing the method? For example:

public class class1{ 
    public void method1(){}
}
public class class2{
    public void method2(){}
}

Now how can I execute method2() everytime when method1() is executed, without changing anything in class1?

Franckentien
  • 324
  • 6
  • 21
RandomGuy
  • 75
  • 8

2 Answers2

4

This is known as AOP or Aspect Oriented Programming, and is implemented via bytecode manipulation.

Spring includes facilities for AOP.

A detailed description of AOP would be too long for StackOverflow, and redundant since there are many good resources on the web.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

Use 'Decorator pattern' to extend functionality of class1. This approach requires change in code that calls class1

Eldar Budagov
  • 312
  • 2
  • 11