Apr 17th, 2009, 07:27 AM
Gradual conversion of legacy code to Spring
I selected Spring as an IoC/DI container because it's supposed to be able to allow piecemeal usage of DI without needing to re-design an entire legacy application.
However, I am having problems doing this.
I understand that if a change a single class to use DI, I can configure that class in Spring and initialize it for instance in the applicationContext.xml file.
Once I've done that though, how can I refer to this instance in legacy code? How do I get a reference to it with the minimum of change to the existing legacy code?
Furthermore, what if the dependencies I'm injecting are themselves initialized in legacy code? How do I retrieve those instances and set them so that Spring has access to them?
Apr 20th, 2009, 03:43 AM
RE: Gradual conversion of legacy code to Spring
You can use singleton like pattern.
1.Insert a static instance field in you spring bean class.
2.Set the value of instance field in the bean constructor.
3.In legacy code use static instance field.
for using legacy classes in bean classes you can hire singleton pattern.
Apr 20th, 2009, 07:17 AM
I don't understand...
I still don't understand how the Spring bean gets a reference to a legacy object instance.
If (and only if) the Spring bean is a singleton, then the legacy class can be modified to reference it and set itself in to the bean.