17/07/2020 java - What exactly is Spring Framework for?
- Stack Overflow
Basically Spring is a framework for dependency-injection which is a pattern that allows building
very decoupled systems.
The problem
For example, suppose you need to list the users of the system and thus declare an interface
called UserLister :
public interface UserLister {
List<User> getUsers();
}
And maybe an implementation accessing a database to get all the users:
public class UserListerDB implements UserLister {
public List<User> getUsers() {
// DB access code here
}
}
In your view you'll need to access an instance (just an example, remember):
public class SomeView {
private UserLister userLister;
public void render() {
List<User> users = [Link]();
[Link](users);
}
}
Note that the code above doesn't have initialized the variable userLister . What should we
do? If I explicitly instantiate the object like this:
UserLister userLister = new UserListerDB();
...I'd couple the view with my implementation of the class that access the DB. What if I want to
switch from the DB implementation to another that gets the user list from a comma-separated
file (remember, it's an example)? In that case, I would go to my code again and change the
above line to:
UserLister userLister = new UserListerCommaSeparatedFile();
This has no problem with a small program like this but... What happens in a program that has
hundreds of views and a similar number of business classes? The maintenance becomes a
nightmare!
Spring (Dependency Injection) approach
What Spring does is to wire the classes up by using an XML file or annotations, this way all
the objects are instantiated and initialized by Spring and injected in the right places (Servlets,
Web Frameworks, Business classes, DAOs, etc, etc, etc...).
[Link] 1/4
17/07/2020 java - What exactly is Spring Framework for? - Stack Overflow
Going back to the example in Spring we just need to have a setter for the userLister field
and have either an XML file like this:
<bean id="userLister" class="UserListerDB" />
<bean class="SomeView">
<property name="userLister" ref="userLister" />
</bean>
or more simply annotate the filed in our view class with @Inject :
@Inject
private UserLister userLister;
This way when the view is created it magically will have a UserLister ready to work.
List<User> users = [Link](); // This will actually work
// without adding any line of code
It is great! Isn't it?
What if you want to use another implementation of your UserLister interface? Just
change the XML.
What if don't have a UserLister implementation ready? Program a temporal mock
implementation of UserLister and ease the development of the view.
What if I don't want to use Spring anymore? Just don't use it! Your application isn't
coupled to it. Inversion of Control states: "The application controls the framework, not the
framework controls the application".
There are some other options for Dependency Injection around there, what in my opinion has
made Spring so famous besides its simplicity, elegance and stability is that the guys of
SpringSource have programmed many many POJOs that help to integrate Spring with many
other common frameworks without being intrusive in your application. Also, Spring has several
good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list
of etceteras.
Hope this helps. Anyway, I encourage you to read Martin Fowler's article about Dependency
Injection and Inversion of Control because he does it better than me. After understanding the
basics take a look at Spring Documentation, in my opinion, it is used to be the best Spring
book ever.
edited Jun 1 at 5:43 answered Jun 30 '09 at 16:04
Minh Nghĩa victor hugo
367 4 15 33.1k 12 63 75
148 Whats the difference between having to change a line of code and a line of XML? The effort and
maintenance hell is exactly the same, or even worse, since the external xml files arguably adds
complexity? Sorry but I just dont get it, I dont see any benefit at all. Please fill me in if Im missing
something. – fred Dec 30 '11 at 5:57
23 @fred - Imagine you are doing unit testing. Without dependency injection(DI can be used with
annotations or with XML) you can't properly test, because you can't mock the dependencies. –
Petar Minchev Dec 30 '11 at 9:32
18 @fred - defining all injection in XML indeed makes little sense. It's a massive overhead to maintain.
Therefor EJB introduced the concept of annotations for injection points. Those are much simpler and
a default instance will be injected (for unit tests this can be changed once). This worked so well that
[Link] 2/4
17/07/2020 java - What exactly is Spring Framework for? - Stack Overflow
Spring has now copied this approach. Note that if needed (but only if really needed) annotations can
still be overriden by XML in EJB. – Mike Braun Jan 12 '12 at 18:45
8 @fred Also, in the real world it would be changing one line of XML vs. a bunch of lines of code. –
victor hugo May 24 '12 at 0:10
36 Or, you know, use a factory static method. Change the factory's return type, and now all of the classes
that use that return value are changed. Presto Spring is now no longer needed... –
Qix - MONICA WAS MISTREATED Nov 25 '14 at 20:04
1 great answer, but I don't think the Spring Documentation is a great book. It didn't start with the basics
as you did here. It would be great if you link a better introductory to Spring. Something that shows
examples as you did to explain the ideas. – Jack Twain Jan 4 '15 at 10:56
1 It used to be, almost 6 years ago when I wrote this answer (wow 6 years!) – victor hugo Jan 5 '15 at
21:24
8 @fred in this example, we see the UserLister object only being used once. So yes, here, it is a
choice of "change one line in XML, or change one line in code". But now, imagine in a larger
application that UserLister is being used in multiple classes / packages. Now it's obvious that you'd
rather change the one line in XML than find every use of the class and change the instantiation (in
potentially a dozen classes) – Don Cheadle Apr 14 '15 at 16:05
16 @mmcrae I'd rather do one refactorization call in my IDE than write XML. –
Qix - MONICA WAS MISTREATED Apr 14 '15 at 16:09
1 @fred I think using a factory in this case would also lead to the same case. But without a factory if
each of your view has x = new y(); then you have to go to each view and change that y to something
else like z. – Prathik Rajendran M Jun 2 '15 at 11:11
Not the best piece of code to justify the need for a new framework. See my answer. – Ash Jun 3 '15 at
2:15
3 @mmcrae Why would you change lots of lines of code? The class instantiates the dependency, so
you'd only replace one line of code for every 'different' line you'd be replacing in XML. I don't support
Spring, nor contest it. – insidesin Aug 4 '15 at 10:39
It seems Spring dependency injection is best suited for CRUD applications – James Wierzba Sep 3
'15 at 14:35
Actually @victorhugo, the framework does control the application...you got it all wrong. –
Jossie Calderon Jun 13 '16 at 5:28
1 @JossieCalderon please elaborate your comment. Otherwise I recommend you to
[Link]/spring/docs/current/spring-framework-reference/… – victor hugo Aug 6 '16 at
14:44
@JamesWierzba absolutely not, it fits for anything that need dependency injection and is expect to
grow. – Walfrat Dec 9 '16 at 9:16
4 Spring is good for people who don't want to spend effort to learn and think and use an api... It yields
absolutely nothing and solves very little. Added complexity of having to load a tone of useless spring
classes and potential memory leaks and learning to understand the framework is a deterent. I see no
use for it other than just saying "yea i know how to write xml configs, hee haw" Anyways 75 up votes
cannot be wrong... Spring is more or less useless library, the only reason people use it is because it's
popular. Almost 10 years working with java I have yet to see a useful implementation – niken May 18
'17 at 18:41
@mamakin So very true... I have seen Spring used in a few real-world projects, but so far it has
always clearly being used for purely political reasons - only adding unnecessary complexity, without
solving any real problems or adding any flexibilty that actually got used. – Rogério Sep 8 '17 at 0:23
I have to add a caveat to my last comment "adds very little" and "solve nothing" are not entirely
accurate statements... While it's true that spring is over-and-miss used a lot, there are places where it
shines and is absolutely invaluable. One such place is transactions and eip. Spring libs give you a
standard approach to orchestrating and enlisting transactions. This functionality is absolutely
invaluable in certain applications... – niken Sep 8 '17 at 15:07
Spring is a framework that aims to reduce the complexity and helps to make a lot of things simpler to
develop In parallel with the above it also helps to reduce the lots of boilerplate code which you
[Link] 3/4
17/07/2020 java - What exactly is Spring Framework for? - Stack Overflow
develop. In parallel with the above, it also helps to reduce the lots of boilerplate code which you
couldn’t get rid of before: The necessary code which is repetitive and draws focus away from the main
logic. Here are some history and approach to why spring was born: [Link]/blog/what-is-
spring-framework-in-java – Zoltán Raffai Jul 9 '18 at 8:02
Also, the guys at spring are software engineering wizards. I'm always impressed at when I read the
spring code at how elegant things are. – xdhmoore Mar 21 '19 at 19:03
@victorhugo flawless answer! – gaurav Jun 21 '19 at 6:18
In my opinion, that was only the starting point of Spring. I met one of the core-developers of Spring
and their intention is a fully-comprehensive Java SE stack alternative to Java EE application
development. – Erdinc Ay Sep 6 '19 at 12:59
[Link] 4/4