In EJB 3.1, I can create a no-interface session bean. When this is injected into other classes, they receive an object that has the same type as my pojo, and yet what they actually get is a stub that by a chain of classes interacts with my pojo. How is this trick pulled off? I could understand if the stub had the same interface type as my pojo, but how does the container create an object of the same type? Reflection? Bit-weaving? Many thanks!
Asked
Active
Viewed 273 times
1 Answers
5
The container generates a proxy class that is a subclass of your no-interface EJB class, and then it overrides all of the methods to do its normal proxying (setup, teardown, and invoking an actual bean instance) rather than calling methods in your instance. Since java.lang.reflect.Proxy doesn't support extending a class, containers have to use another approach to generate a class, likely using a bytecode library like ASM, BCEL, Javassist, etc.

Brett Kail
- 33,593
- 2
- 85
- 90
-
1Ah great - thanks for your fantastic answer. Been wondering that for a while..! – user384842 Nov 18 '14 at 15:45