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.