Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Spring Framework Overview

In this article we will talk about Spring Framework in Java.

Spring framework is an open source, light weight application development framework for enterprise Java. The core features of the Spring framework can be used to develop java applications. By using extensions, web applications can be built on top of the Java EE platform. The Spring framework is considered to be light because of the size. The spring framework is around 2MB (for the basic version). Spring is also used vastly in the developer community because it promotes good programming practices by encouraging POJO based development.

Benefits of using Spring Framework

The following are some of the key benefits of using the Spring framework.

  • Spring encourages POJO based development. The key benefit of using POJOs is that it is enough to have a Servlet container like Tomcat instead of having an expensive application server with EJB containers.
  • Spring reuses technologies like Quartz, ORM , JDK timers, logging frameworks, etc instead of creating technologies of its own.
  • Spring web-framework is a well-defined web MVM framework. That means it is no longer required to use other web frameworks like struts.
  • Since Spring is lightweight, with lightweight IoC containers, a normal system with normal memory configurations is enough to develop and deploy Spring based application. The same is not true with heavyweight EJB containers.
  • Spring has a consistent transaction management interface that can be used for both local and global transaction managements.
  • Testing Spring based applications is easy. Since it is POJO based, injecting test data become easy through dependency injection.
  • Spring is modular in nature. For example, you may use spring for simplifying the JDBC calls or just for managing the business objects. Thus a developer may use some modules and completely eliminate the remaining modules.

Spring Architecture

Spring framework is layered and has many modules. As discussed earlier, the modularity of the framework ensures that the developer can pick and choose the modules to be used and leave out the rest. The Spring architecture includes many modules for a variety of features required to develop an enterprise application. The modules also ensure a seamless integration of the spring framework with other frameworks like Struts, JSF, Hibernate, etc.

Spring Architecture Diagram

Figure 1: Spring Architecture Diagram

The Core: The core module takes care of Dependency Injection (DI), the most important feature of Spring Framework. The module includes a beanFactory that creates beans as per configurations specified in the XML file. This follows the Factory Pattern. There is an expression language module within the core container that provides strong expression language for querying and manipulating an object graph during runtime. The core module also creates the ApplicationContext, a medium to access any object defined and configured.

Aspect Oriented Programming Module

The Aspect Oriented Programming (AOP) module allows developers to define point cuts to decouple code that implements certain functionalities. The Aspect module allows integration with AspectJ which is a powerful AOP framework.

DAO Module

This module hides the lower level data access tasks like creating the connections and releasing them. This module also maintains a hierarchical list of meaningful exceptions instead of throwing database specific complicated errors. Transactions are managed using AOP. It is also possible to programmatically manage transactions.

ORM Module

Spring in itself doesn’t provide any ORM implementation. However, it allows for easy integration of other ORM tools like Hibernate, Oracle TopLinks, iBatis SQL Maps, JPA and the like. If the project doesn’t require ORM integration, this module can be completely ignored.

JEE Module

Spring framework can also provide support for Java Messaging Service (JMS), Enterprise Java Bean (EJB), Java EE Connection API (JCA), Java Management Extension (JMX), remoting, etc. The JEE module allows for seamless integration with all the mentioned technologies and more.

Web Module

Spring has a well-developed MVC framework that allows for easy development of web application. It also allows for easy integration with other popular frameworks like Struts, JSF, Tapestry, Velocity, JasperReport and others of the like.

Spring Dependency Injection

Dependency Injection (DI) is the most important feature of Spring framework. A general concept of Dependency Injection is called the Inversion of Control (IoC). Inversion of Control means that a class should not configure itself. Instead, it should be controlled/configured from outside. It is always better to keep the classes as independent as possible. Dependency injection helps achieve that. Instead of having a class calling another class through code, dependency injection injects the dependency at run time. For example, if class A is dependent on class B, then the container injects class B into class A during runtime. Class B can be injected into class A through one of the two options

  • The constructor of the class A, which is called construction injection
  • A setter method, which is called setter injection

No matter which of the above is used, dependency injection ensures that classes remain independent and separate as much as possible.

In Spring, dependency injection is carried out by the Spring IoC container. The container injects the required objects based on Bean configurations.

packagecom.src.sample;

importcom.src.sample.api.IGenre;
 
publicclassPlaylistCreator
{
	IGenregenre;
 
	publicvoidsetGenre(IGenregenre){
		this.genre=genre;
	}
 
}

The class above has a setter with IGenre parameter. The PlaylistCreator is dependent on Genre to create the playlist.

Here is the Spring configuration for DI.

<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<beanid="PlaylistCreator"class="com.src.sample.PlaylistCreator">
		<propertyname="genre">
			<refbean="ClassicGenre"/>
		</property>
	</bean>
 
<beanid="ClassicGenre"class="com.src.sample.impl.ClassicGenre"/>
<beanid="CountryGenre"class="com.src.sample.impl.CountryGenre"/>
 
</beans>

The above configuration injects the implementation, ClassicGenre into the PlaylistCreator through the property genre. By simply changing the configuration, the genre can changed from ClassicGenre to CountryGenre.

Dependency injection isn’t entirely a Spring concept. It can also be achieved directly through java. However, Spring has made dependency inject much easier and simple to use.

Creating objects around dependency injection allows quick application testing. For example, if a class needs a data access object, during testing, it is possible to easily mock the DAO by injecting a mock DAO without having a database . Thus it saves time and cost by simulating complex set up, merely by injecting desired mock objects.

Aspect Oriented Programming (AOP)

One of the important modules of Spring framework is the Aspect Oriented Programming (AOP) framework. A function that cuts across multiple points in an application is called cross-cutting concerns. Cross-cutting concerns are conceptually different from application’s business logic. There are many examples of cross-cutting concerns that are commonly used in many projects. Few of them are logging, security , declarative transaction, etc. These cross cutting functionalities span across multiple points and aren’t really part of the business logic. In Object Orient Programing (OOP) the central unit of modularity is the object. With Aspect Oriented Programming (AOP), however, the central unit of modularity is Aspect. Dependency Injection (DI) ensures that the classes are independent of each other. Aspect Oriented Programming (AOP) ensures that functional code is independent of cross-cutting concerns. The Aspect Oriented Programming enables you to clearly define method-interceptors and point cuts to decouple functional code from cross cutting concerns.

AOP is also used in Spring to provide declarative enterprise services, especially as a replacement for Enterprise Java Bean (EJB) declarative services. Most important one of such service is the declarative transaction management. The Spring’s declarative transaction management is similar to EJB CMT. However, the declarative transaction management in Spring can work in any environment while EJB CMT works with JTA. As in EJB CMT, with Spring, the transaction behavior or lack of transaction behavior can be defined down to the individual method level.

That is all for today’s article. See you next time



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login