Thursday, February 5, 2009

So your having trouble with your Flex Profiler?

If you are having socket connection problems from your profiler console try removing your: (Windows XP)

C:\Documents and Settings(login)\My Documents\Flex Builder 3\.metadata\.plugins\com.adobe.flash.profiler\ProfilerAgent.swf

This file will then be recreated when you next attempt to run a profiled session.

Sunday, February 1, 2009

Spring Method Injection

This past week I came across my first need to intermix Spring beans with different lifecycles.

Previously every bean in our Spring container was a singleton, but now I needed a singleton to dynamically allocate new beans of a different type who each needed a reference to another singleton. The exact situation is of a singleton manager class who creates many new objects. Each of these dynamic objects in their own processing needs access to a singleton data access object in order to populate all of its own properties.

Previously my dynamically created object was not in the Spring container and my
first thought was to leave it as such and access the singleton DAO bean by asking the container for it. This would work by implementing the BeanFactoryAware interface; however the Spring documentation does not recommend this as you would then have more direct dependency on the Spring implementation (Something that Spring itself tries to minimize).

My second thought was just to inject the DAO bean into my manager bean and pass the DAO to each of my dynamic objects after they were created to use as needed. This also would have worked but I didn't like the idea of wiring a bean to another bean who didn't need it.

The recommended approach is to use method injection described in the Spring documentation.
I used this approach and it worked exactly as advertised. Here is what I had to do (as shown in the doc link):

  1. Add my dynamically created bean to the spring container with a scope of "prototype". This will cause the container to return a new object each time it is inected:
  2. Inject the DAO bean into this dynamic "prototype" bean (with setter injection).
  3. Create a method in the manager object to return a new dynamic bean. This is called each time a new bean is requested in the place of where it was previously allocated. This is the method that is injected but the complier needs a method body so I just had mine return null.
  4. Add the spring method injection logic to the container configuration file. (As shown in the docs). Then at runtime when the manager class the creation method for a new dynamically created object the Spring logic that was inject takes over to create and return a new "prototype" scoped bean.