JAVA EE proxy pattern

2.1k views Asked by At

I keep reading everywhere that when you ask for dependencies to be injected in a bean, you are injected a proxy to an instance of that resource. I believe I know what a proxy is, its an instance that knows how to forward messages to another instance. It's also stated that it is this pattern that allows the container to provide services to this managed beans.

I don't understand this quite well. Why is a proxy necessary? And how is this implemented? is there a proxy object for each bean? or do i have many proxies forwarding to one instance? or maybe neither?

Also, from the book design patterns from the GoF, I've read that you have to provide a proxy class that acts as a placeholder. But I never do that in Java EE, does the application server create proxy class during runtime?

1

There are 1 answers

2
Martin Klinke On BEST ANSWER

The essential assumption behind the proxy pattern is that you should not have to care about it from a "user" point of view. The proxy masquerades as the declared type and should behave the same way. The added value comes from what the proxy does before or after it forwards or returns calls to the target instance. This is how e.g. transactions and security are implemented in the container.

As for the added question: Yes, the application server creates those proxy classes when necessary.