Use scope
- Java AOP is base on dynamic proxy
- Plug in, for increasing function of package already exists.
- Framework, for some abstract programming level.
Demo
1. an Interface
1 | public interface Interface { |
- first interface used to call a method
that has no arguments and return - second interface used to call a method
that has a argument and return string
2. an implements class
1 | public class HelloProxy implements Interface { |
- Geet to world
- Geet to the input argument and return
3. an InvocationHandler
1 | import java.lang.reflect.InvocationHandler; |
The handler implements InvocationHandler is the key
to dynamic call the target methods or fields.All the call in handler would be redirect to a
call processor
.The first argument in method invoke is used to deal the
manual call object.The second argument is the method you call in a
uppper reference or a maunal call method.The third argument is the arguments you want to input,
the processor would distinguish the method sign may be -_-.
uppper input or maunal input arguments.
4. have a try
1 | import java.lang.reflect.Proxy; |
The
newProxyInstance
will create a subClass of the second argument
so the$proxy0
can be transform to theInterface
.The first argument
classLoader
is to point which class is agent.The third argument is the handler we created in step 3
take a proxied hello interface implement into it’s constructor.The result in
sayHello
has been deal by handler.
Result:
Hello world
Hello proxy
Invoke Hello proxy