Results 1 to 3 of 3

Thread: Gradual conversion of legacy code to Spring

  1. #1
    Join Date
    Apr 2009
    Posts
    8

    Question 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?

  2. #2
    Join Date
    Jan 2009
    Posts
    17

    Default 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.

  3. #3
    Join Date
    Apr 2009
    Posts
    8

    Question 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •