Hello Friends, this is my very first Article on Introduction to Spring under “Spring Tutorials” category. Drop your comments and feedback below to help me write more on Spring in abetter and improved way.
Before diving into the Introduction to Spring part, let me explain you why I thought of starting writing “Spring Tutorials”. I am a Java developer and there was a requirement in my project for Spring MVC (Model View Controller) framework. Earlier, I was working with “Java Server Pages (JSPs)” and “Servlets”. Then I upgraded my entire project flow to Spring MVC. Since, I really found this technology worth the efforts. So, I thought of sharing my knowledge with you members. I found “PhpRinG – People Helping People” website a great source of learning for beginners. I am glad to have this platform and share my thoughts freely.
Introduction to SPRING.
Spring Yesterday, Today and Tomorrow :-
Rod Johnson introduced his interface 21 framework which he used in his own applications, in the book Expert One-to-One J2EE Design and Development. When it was released into the open source world, this frame work formed the foundation of Spring framework. Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, reusable code.
While working with Java Server Pages (JSPs) and Servlets, I faced a number of challenges. I always wondered –
- Can I have a simpler solution?
- How I can handle my ever changing requirements?
- How can I make the architecture flexible?
- Can’t I have everything in components which can be verified independently?
- Can I make plug and play software?
- How can the software components be reusable and changeable?
- Should I use Enterprise Java Beans (EJBs)? as it makes the product hard to maintain, more complex, reduces predictablitity and everything is framework dependent.
- And many more questions like these…
I found SPRING was the ANSWER to all these Questions !!!
Now, let’s see what this Spring is and how it can make your Java life easier. Wiki summarizes Spring as- “An Open Source application framework and Inversion of control container for the Java platform”.
Did you understand this definition? If your answer is YES, then you are good to do with Spring and If your answer is NO, then you are like ME. It’s not a big deal to learn anything new; the big deal is to make passion for leaning. Let’s learn it together and Create our own definition like Wiki did.
Scenario before spring was introduced to market :-
When web applications were in hype, Servlets and JSPs had solved innumerable problems. Servlets are great for writing Java code, acting as a central point for managing application flow and invoking business methods, but are horrible at presentation; JSPs are great at presentation but are a horrible place to embed all of your business logic. In 1998, Enterprise Java Beans (EJBs) were in limelight because companies were indulged in Enterprise web development, Developers led by Rod Johnson created the Spring Framework so that our application should focus on solving its business problems rather than on the overhead required to construct resources and objects. Spring creates objects for you and makes them available at runtime.
When I started with Spring, two concepts which were repeatedly used were IOC and DI.
- IOC – Inversion of Control.
- DI – Dependency Injection.
To understand these two concepts, let’s take this scenario :-
Public Class Phpring
Private Ring ring ;
Public Phpring ()
ring =new ring ();
Here, we have a Phpring class which contains an address class object. The Phpring class depends on the Ring object. So if, for any reason Ring class changes it will lead to change and compiling of Phpring class also which is whole lot a big process and will take good amount of time. Moreover you can clearly see the code is tightly coupled since Ring class is directly referenced in the Phpring class. So if for any reason the ring object fails then whole Phpring class will fail in the constructor initialization itself. This is what we used to do when IOC was not in picture. Right?
Now, with IOC let’s see what is happening… IOC means Inversion of Control which clearly says that the control is inverted. Inverting the control means giving the control to somebody else i.e if we give the control of object creation to somebody else here then the problem will be solved. Phpring class doesn’t have to bother about creating the Ring object. Won’t this solve our problem to a large extent?
So the basic funda of IOC is, the main class (here Phpring class) should not depend on any other classes (here Ring class) rather they can depend on an abstraction either using Interface or abstract class. Hence, abstraction is very well preserved!!! OOP …yeah!
Working of IOC (Inversion of Control) :-
Interestingly we can also notice that with IOC we have such a decoupled code. Did you also notice dependency injection here? The class Phpring was dependent on class Ring i.e Ring class was a dependency for class Phpring so what we have done here is injected the Ring object into Phpring class. Hence, we can conclude that Inversion of Control is a principle and Dependency Injection is a way of implementing Inversion of Control.
This is just a glimpse to these 2 core concepts which are heart of Spring. There is one more concept associated with Spring very strongly which is enjoying absolute limelight these days – Aspect Oriented Programming. In my next article, I’ll elaborate these two concepts IOC and DI as well as we will get into AOP-Aspect Oriented programming.
As mentioned above “Spring creates objects for you and makes them available at runtime.” Now I believe we can relate more to this. In our example above, we have also seen some remarkable benefits of Spring .
Briefing down some takeaway points :-
- Lightweight :- The basic version of Spring is just 2MB so we can develop and deploy applications in systems with limited CPU memory. And again if we say lightweight ,it means we have to make few, if any, changes to our application code to gain benefits of Spring core and if we want to quit the usage of Spring at any point of time then that can also be done easily.
- Enables developers to develop Enterprise-class applications using POJOs :- With Enterprise Java Beans the problem was a number of interfaces and configuration files which is why it was tedious, awkward and prone to errors. Using POJOs you won’t need any EJB container rather you can use servlet container like Tomcat or something which is quite robust to manage the instantiation and lifecycle of our POJO components.
- Organized in a modular fashion :- Yes, Spring framework is organized such that you have to concentrate only on the part you have to change and not anything else.
- Does not reinvent the wheel meaning Spring uses the existing technologies like ORM framework, logging framework, JEE, Quartz, JDK timer, etc. We can say Spring is a framework of frameworks. Spring supports two job scheduling mechanisms: one uses Timer class which has been available since Java 1.3 and other uses Quartz scheduling engine.
- Testing made simpler :-The Enterprise Java Beans container concept made unit testing next to impossible. But using DI, injecting test data is much easier. Each component in Spring is testable with minimal or no intrusion from outside classes.
- Reduce glue Code :- Dependency Injection dramatically reduces the amount of code we have to write to glue the different components of our application together.
- Exception handling :- A platform should not force you to catch exceptions you are unlikely to be able to recover from. Checked exceptions are a overhead in Java. Spring provides a convenient API(Application Programming Interface ) to translate technology specific exceptions(thrown by JDBC, Hibernate, JDO, etc) into consistent, unchecked exceptions. With Spring, Exception hierarchy is remarkably granular making it easy to identify, classify and diaogonise errors within your application and reducing the amount of code you have to write.
- Consistent transaction management :-Spring provides excellent abstraction layer for transaction management, allowing programmatic and declarative transaction control. You can start with simple, local, resource specific transactions and move to global, multi-resource transactions without having to change your code.
Free e-books and Pdf’s :-
Please Subscribe or Like PhpRinG Tutorials for free e-books and pdf’s.
- Launching Very Soon free e-books and pdf’s on Spring.
In this article, I tried to present you Introduction to Sprig and a high level view of the Spring framework. We talked about IOC (Inversion Of Control) and DI (Dependency Injection). We now know the two major concepts of Spring – it;s past and present. We can now summarize what Spring can do for us and how it will do, will be discussed in coming articles. I hope you like my article on Introduction to Spring. Your feedbacks and Comments are welcome below.
VN:F [1.9.22_1171]Introduction to Spring - First Hello Spring Then Hello World in Spring,