tags:

views:

238

answers:

2

This is a short one: I want to modify the way spring instantiates my beans (in this case, by creating a proxy instead of simply instantiating it).

I can't use Jdk Dynamic Proxies nor cglib to generate the proxies (if this was the case, I could use spring aop).

In my opinion, the best way to do this would be to extend spring's bean factory(and tell spring to use my factory), but I can't find how to do it on Google.

Any thoughts about this?

edit:

Actually, looks like the most standard way to do this is to use a BeanPostProcessor(http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/BeanPostProcessor.html), and make that PostProcessor wrap the bean on a proxy.

If we can't get to a better solution around here (and we decide this is a valuable question), I'll post this as the answer.

A: 

You just have to extend AbstractApplicationContext.

That's the easy part.

The hard part that I'm having trouble with is: If you can't use dynamic proxies or CGLIB, what method do you plan to use?

My advice? Don't. The "special need" that you're imagining is a fiction, and you won't be able to come up with a better way than Rod Johnson has already provided for you.

If you can post a lucid explanation that convinces me I'll delete my answer.

duffymo
I'm maintaining a huge 'kind-of legacy' code base. I can't use CGLIB because my Controller beans don't have empty constructors (all constructors are @Autowired to inject other Controllers), and can't use Dynamic Proxies because I don't want to generate interfaces for all controllers just to fulfill the requirement to generate this kind of proxy (I know interfaces should be used from the beginning, but this is simply not our reality). I intend to set a MethodInterceptor around the beans to intercept the method calls and use on Aspectual situations, like logging.
Rodrigo Gama
Anyway, it's not my intention right now to make a better general solution than the one Rod Johnson provided. I Just intend to come up with one more suited for my needs.
Rodrigo Gama
-1 Extending AbstractApplicationContext would force you to provide implementations for methods dealing with BeanFactory that are already implemented in its concrete subclasses.
ChssPly76
+2  A: 

The way to go here is to implement a BeanPostProcessor(http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/BeanPostProcessor.html), and make that BeanPostProcessor wrap the bean on a proxy, on 'postProcessBeforeInitialization'.

Rodrigo Gama