问题
In a Java EE 7 application, let's say I have a session bean class "@Stateless public class MyEJB
". When debugging under Glassfish 4, I can see that a proxy subclass named MyEJB$Proxy$_$$_Weld$EnterpriseProxy$
was created (in order to provide Java EE container services to the EJB).
I am trying to figure out how these proxy subclasses are implemented. Is there a standard Java EE SPI which the proxy implementation calls, for example, to demarcate a transaction? Or is this done through container-specific internal APIs only?
(My motivation for this question is so that I could implement a portable CDI extension to enable true POJOs and OO programming in a Java EE context.)
回答1:
Proxy implementations are created by the ProxyFactory
class. You can have a look at the source in the Weld Github repo.
Basically proxy classes look like this:
public class Test_$$Proxy extends Test
{
public void doSomething(int someValue)
{
Testinstance = lookupBean();
instance.doSomething(someValue);
}
private Test lookupBean()
{
//get the correct instance from the BeanManager and return it
}
}
Plus a lot of extra magic. Maybe you can get some details from the CDI spec and from this article.
来源:https://stackoverflow.com/questions/22465410/how-is-a-proxy-weldenterpriseproxy-proxy-subclass-implemented