0

What is the main advantage of the Spring framework?

Is it loosing dependence between objects ? We can setup the values of fields in xml files but what is the difference between initializing these fields with help xml or simply in the java code?

Here is very similar question: What exactly is Spring Framework for?
Is there any other advantage ?

Community
  • 1
  • 1
Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73

1 Answers1

1

This is a big topic but I'll try to explain Inversion Of Control

An example: Say that you have a project where you need to communicate with different third parties to whom you need to communicate, but the details differ.

The best way would be to define a generic interface

public interface ThirdPartyConnector{
    public String sendRequest(String data);
}

Implementation for your first third party could look:

public class FirstThirdPartyConnector implements ThirdPartyConnector{
    public String sendRequest(String data){
    //implementation details
    }
}

Your second third party:

public class SecondThirdPartyConnector implements ThirdPartyConnector{
    public String sendRequest(String data){
    //implementation details
    }
}

And so on...

Now, in your java code, when communicating with these third parties, you never instantiate them with the new keyword, you work with ThirdPartyConnector interface. This way, during compile time you don't specifiy in your code which ThirdPartyConnector to use.

You do that in XML.

And not changing Java code to switch implementation is crucial in big systems because of complexity in maintenance. And you don't need to touch previous implementations to implement new third parties.

isah
  • 5,221
  • 3
  • 26
  • 36