15

At work and online I keep hearing the term "proxy" with respect to enterprise Java development. For example, metrics-spring uses this phrase:

This module does the following things:

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

I'm unfamiliar with a lot of the language in the Java ecosystem of frameworks and libraries. I feel like I have a good understanding of what a bean is, but I'm still not clear about how one would proxy a bean.

What does it mean to proxy a bean?

Community
  • 1
  • 1
Cory Klein
  • 51,188
  • 43
  • 183
  • 243

3 Answers3

20

Typically, you have a bean like

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

With this, you can do anything that the Bean class declares as behavior (invoke its methods).

There are times where it would be nice if you could, for example, track how long a method invocation takes.

You could do

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

But doing this for each method invocation sucks. So, instead, patterns, architectures, and styles like Aspect Oriented Programming exist.

Instead of the Bean above, you'd have

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

where the TimingBean is a proxy type that extends and implements all the types that Bean extends and implements. For all intents and purposes it is a Bean, but it adds a bunch of additional behavior before delegating each invocation to the Bean object. In this case, it would track how long each of Bean's method's took to execute.

Basic Spring uses JDK proxies and CGLIB proxies. Here are some differences between them.

It uses this for its scheduling and asynchronous invocations. It uses it for transactional support with databases. It uses it for caching. It even uses it for its Java based container configuration.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Proxying means that your client code thinks it's talking to one bean, but a proxy is really doing the listening and responding.

This has been true since early distributed client/server computing models like CORBA. A client would interact with an 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 marshalling request data into a request, communicating over the network to the remote object running on a server, and unmarshalling the response back to the client.

Spring uses this model for remoting. It also forms the basis for its aspect oriented programming model. Your code thinks it's dealing with a particular interface; Spring can weave in advice before, after, or around that instance and perform cross cutting operations like logging, transaction management, etc. on your behalf.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Some frameworks rely on a mechanism called instrumentation, which in short means to build a proxy of a given compiled bytecode, adding some code to it in some places we judge useful. This would implement many kinds of tasks, between them for example, adding a kind of profiling to a spring bean, as this library claims to do.

The Spring engine returns heavily instrumented proxies of every managed bean it offers - this way, you can use Spring declarative transaction handling, for instance. You would write "naive" daos without an actual connection handling, and "naive" service classes using the daos without an actual transaction handling - the instrumented proxies will include the boilerplate code with the connection instantiation, commits, rollbacks...

I hope this is of any help

Jorge_B
  • 9,712
  • 2
  • 17
  • 22