I recently posted about getting started with Java Spring framework. Having worked with various frameworks, I am interested in picking up Spring.
So, I tried a simple hello world application using Spring. Here in this post and followup posts, we will see Spring framework from scratch.
If you have worked with Java J2EE (before EJB 3.x), you will appreciate Spring much. In older J2EE to use EJBs you would have to define/deploy several interface/proxies. There was a Home Interface, Remote and local interfaces. See this old post for more on these interfaces. It was a well meaning architecture that quickly became so complex. EJB 3.x specification finally simplified this to some extent, but only after borrowing heavily from frameworks like Spring that simplified enterprise Java development.
So, the developer has the option to go with EJB 3.0 or Spring (actually, there are lot more options available See here for top 10). But, there has been a lot of buzz around Spring for a while. They keep adding packages that are more modern. So, without going into the debate of EJB 3.0 or Spring, I will dive into Spring for now.
As said above, Spring simplified Container technology by introducing IOC and Dependency Injection. IOC made the containers light weight (compare that EJB 2.x containers) and we could write Java programs with POJOs.
At the core of Spring is a (Java) Bean. This is not unlike Struts or ATG Nucleus. The bean life cycle is managed using a Bean Factory – essentially a Factory service that can create objects. The object names are “injected” into the Factory using metadata in XML. Thus, the object creation is completely decoupled from the application.
The Bean Factory is extended to a Application Context interface, which makes it easier to work with application level services such as Spring AOP, messaging etc. This ApplicationContext interface then forms the IOC container* in Spring framework.
Spring Container or Framework?
Spring is often referred to as a light weight container. This is debatable , but the idea is you don’t need the entire Spring Stack to use parts of the framework. This is unlike the J2EE framework that needs the entire set of classes loaded to even run a small application. In another context, because of DI, Spring is considered light weight, as the object life cycle is managed from within the Java program and not some gigantic software (like Weblogic) that runs on the OS. Others say, it’s not a container, but just a framework that implements DI. So, depending on the perspective Spring may or may not be a container and it may or may not be light weight. (Google’s Guice etc).
Whatever it is, Spring framework contains several different modules in one. You can pick and choose various parts of it and use them independently. Spring Core and Spring AOP is part of core components required to build a Spring application. You can use Spring MVC to build a web application. You use Spring Boot or Spring Roo to generate/build Spring applications quickly using some tools. Spring Batch is used for Spring Programs running in a batch environment. This is another reason it’s called lightweight.
Here is the complete picture of the Spring framework at runtime:
To be contd…