What does "proxy a bean" mean?

At work and on the Internet, I continue to hear the term "proxy" when referring to Java development in the enterprise. For example, metrics-spring uses this phrase:

This module does the following:

Creates metrics and proxy beans that contain methods annotated with @Timed, @Metered, @ExceptionMetered and @Counted [emphasis mine]

I am not familiar with a lot of language in the Java frameworks and libraries ecosystem. I feel like I have a good understanding of what a bean is , but I still don't understand how to proxy a bean.

What does a bean proxy mean?

+3


source to share


3 answers


Typically you have a bean like

Bean bean = new Bean(); // actually created by the context

      

With this, you can do whatever the class Bean

declares as behavior (call its methods).

There are times when it would be nice if you could, for example, keep track of how long a method call takes.

You could do

long start = .. // get start time
bean.invoke();
long end = .. // get end time
// end - start

      



But doing it for every method call sucks. So, there are templates, architectures and styles instead, such as Aspect Oriented Programming .

Instead of the Bean

above, you will have

Bean bean = new TimingBean(new Bean()); // again done by the context

      

where TimingBean

is a proxy type that extends and implements all types that are Bean

extended and implemented. For all purposes and tasks, hea Bean

, but adds a bunch of extra behavior before delegating each call to the object Bean

. In this case, it will keep track of how long each method took Bean

.

Main Spring uses JDK proxies and CGLIB proxies . Here are some of the differences between the two .

It uses this for scheduling and asynchronous calls . He uses it for transactional support with databases . It uses it for caching . It even uses it for java container configuration .

+5


source


Proxying means your client code thinks it is talking to a single bean, but the proxy is actually listening and responding.

This is true with early distributed client / server computing models such as CORBA. The client would interact with the interface type as if it existed in their memory space, but they were really talking to a proxy that would handle all the messy details around sorting the request data in the request, communicating over the network with a remote object running to the server and cancel reply back to customer.



Spring uses this model for remote access. It also forms the basis for its aspect-oriented programming model. Your code thinks it deals with a specific interface; Spring can interweave in tip before, after, or around that instance and perform cut-off operations like logging, transaction management, etc. on your behalf.

+1


source


Some frameworks rely on a mechanism called "toolkit" which, in short, to create a proxy of a given compiled bytecode, adding some code to it in some places that we find useful. This will allow many tasks to be implemented, for example in between, by adding some sort of profiling to the spring bean as this library claims.

The spring mechanism returns strongly managed proxies from all the managed beans it offers - this way you can use spring declarative transaction for example. You will write "naive" daos without real connection handling and "naive" service classes using daos without real transaction processing - instrumental proxies will include boilerplate code with connection creation, commit, rollback ...

I hope this helps.

0


source







All Articles