Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:51:21+00:00 2026-06-18T08:51:21+00:00

I’m trying to change some legacy code to use DI with Spring framework. I

  • 0

I’m trying to change some legacy code to use DI with Spring framework. I have a concrete case for which I’m wondering which is the most proper way to implement it.

It is a java desktop application. There is a DataManager interface used to query / change data from the data store. Currently there is only one implementation using a XML file for store, but in the future it is possible to add SQL implementation. Also for unit testing I may need to mock it.

Currently every peace of code that needs the data manager retrieves it by using a factory. Here is the source code of the factory:

public class DataManagerFactory   
{  
    private static DataManagerIfc dataManager;

    public static DataManagerIfc getInstance()
    {
        // Let assume synchronization is not needed
        if(dataManager == null)
            dataManager = new XMLFileDataManager();

        return dataManager;
    }
}

Now I see 3 ways to change the application to use DI and Spring.

I. Inject the dependency only in the factory and do not change any other code.

Here is the new code:

public class DataManagerFactory  
{
    private DataManagerIfc dataManager;

    public DataManagerFactory(DataManagerIfc dataManager)
    {
        this.dataManager = dataManager;
    }

    public DataManagerIfc getDataManager()
    {
        return dataManager;
    }

    public static DataManagerIfc getInstance()
    {
        return getFactoryInstance().getDataManager();
    }

    public static DataManagerFactory getFactoryInstance()
    {
        ApplicationContext context =
                    new ClassPathXmlApplicationContext(new String[] {"com/mypackage/SpringConfig.xml"});

        return context.getBean(DataManagerFactory.class);
    }
}

And the XML with the bean description:

<bean id="dataManagerFactory"
            class="com.mypackage.DataManagerFactory">
    <constructor-arg ref="xmlFileDataManager"/>
</bean>

<bean id="xmlFileDataManager"
    class="com.mypackage.datamanagers.xmlfiledatamanager.XMLFileDataManager">
</bean>

II. Change every class that is using the data manager so it takes it through the constructor and store it as a class variable. Make Spring bean definitions only for the “root” classes from where the chain of creation starts.

III. Same as II. but for every class that is using the data manager create a Spring bean definition and instantiate every such class by using the Spring Ioc container.

As I’m new to the DI concept, I will appreciate every advice what will be the correct and “best practice” solution.
Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:51:23+00:00Added an answer on June 18, 2026 at 8:51 am

    Use option 3.

    The first option keeps your code untestable. You won’t be able to easily mock the static factory method so that it returns a mock DataManager.

    The second option will force you to have the root classes know all the dependencies of all the non-root classes in order to make the code testable.

    The third option really uses dependency injection, where each bean only know about its direct dependencies, and is injected by the DI container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.