PDA

View Full Version : Bean Method Injection



dougman82
Apr 28th, 2011, 11:54 AM
So, to simplify things... let's say I've got a ProcessorBean that does some simple data processing. Then, I have some other beans BeanA, BeanB, and BeanC that need to use that data processing. So currently, in my XML configuration file, I wire up those other beans so that they have a reference to ProcessorBean. Then, inside those other beans, I can simply call the relevant methods of ProcessorBean to get the necessary data.

Well now, I am working with a 3rd-party class of which I do not have access to the source. This class also needs to utilize the data processing provided by ProcessorBean. However, since I do not have the source, I cannot adjust it to accept an instance of any other bean. All I have to work with are some basic setter methods.

Is there a way, upon creation of this new bean, to set a property as the return value of a method in ProcessorBean? I took a look at MethodInvokingFactoryBean, and it seems like it will get the job done, except that, unless I'm mistaken, it creates a whole new instance of the bean in question. ProcessorBean already exists and is being used elsewhere in the project. I just need to access a method. Can this be done?

Any help is appreciated.

dougman82
Apr 28th, 2011, 12:16 PM
I guess I could just create a go-between bean that would take a reference to both the 3rd-party bean and ProcessorBean. Then it could get the data from ProcessorBean and use it to set 3rd-party-bean's stuff. Is there a more elegant way than this?

Enrico Pizzi
Apr 29th, 2011, 02:41 AM
I think the best way is to implement a custom bean post-processor to use for the third party bean. In the post-processing, you could call the bean's unhortodox setters to inject the wanted resource.