Spring Framework Introduction


AOP (Aspect-Oriented Programming)

In computing terminology, aspect-oriented programming (AOP) is a programming style which shields supporting code (like logging etc.) from the main program’s business logic code. It’s target is to increase modularity, by permitting the separation of secondary code that cuts across the main code. The main code includes the actual business logic and the secondary code makes the business logic more robust like including the code that should be executed when a business logic fails. In AOP, this extra code is said to cross cuts the actual code.

What is concern in AOP?

Aspect-oriented programming breaks the whole logic into discrete (separate) parts known as concerns (like marketing, finance, production, planning etc). Every programming style supports these concerns by implementing them in the code thorough functions, classes, methods and modules etc. Another style of concerns exist that come across these concerns (other way which cuts across) called cross cutting concerns like logging, transaction management, security etc. AOP is an engineering discipline or engineering paradigm.

What is join point in AOP?

After knowing what a concern is, let us know what is join point. The place in the main code where the concern (code) is inserted is known as join point. The place where this extra code is inserted in the main logic is known as join point. For example, the same security check up may required to be inserted in three places in the main code, we say, there exist three join points for security permissions.

What is pointcut in AOP?

A pointcut is a set of join points or a pointcut describes the join points.

What is advice?

When the execution comes across one of the join points as said in the pointcut, a small slice of code associated with pointcut is executed. The slice of code is known advice.

What is aspect?

The programmer can describe where he requires the additional code in the main logic. The advices added are known as aspects. Aspects make clear separation between main logic and concerns.

Spring Architecture

A) The Spring framework overview

As on today, Spring is much demanded framework that reduces the complexity of developing of Java enterprise (J2EE) applications. Spring comes with 7 modules wherein Core module is the basic one. Spring adds the advantage of loose coupling to server-side Java components. Developer is allowed to use modules in his coding.

B) Brief introduction to Spring framwork modules

Among the Spring’s layered architecture of 7 modules, the Core container (module) allows the programmer to create, configure and manage the JavaBeans. Spring framework is known as lightweight container as it can be executed from the DOS prompt also without depending on the Web container (used by Servlets) or Enterprise container (used by EJBs).

Spring Framework Introduction

1. The core container

The main classes required for IoC Spring framework exist in Core container (module). The key component in the Core container is BeanFactory. The BeanFactory belongs to Inversion of Control (IoC). IoC configures the dependency injection (DI). The DI separates the configuration from the application code.

2. Spring AOP module

The AOP module loose couples the concerns like transaction management from the main code. AOP permits to inject the aspects in the main code at different join points.

3. Spring DAO module

DAO module describes the JDBC abstraction layer. It relieves the programmer from writing tedious database specific SQL code. Lot of JDBC and concerns are written using declarative management. Declarative management avoids the traditional code with just declaring in an XML file (known as configuration file).

4. Spring ORM module
The advantage of Spring framework is it can be integrated to any ORM you like, may be popular Hibernate or others like iBATIS and JDO etc. Support to ORM along with transaction management makes Spring a much wanted enterprise framework as on today.

5. Spring Web module

The Spring Web context module provides the necessary classes required to integrate Spring with Servlets, JSP and Struts etc.

6. Spring MVC framework module

The advantages of Spring framework continues. It allows your own MVC components (like Struts) to integrate with it or use it’s built in MVC module (without the necessity of Struts). With Spring MVC module, the view need not be JSP or HTML file, it can be XSLT or Velocity templates generated view to client. Spring supports Velocity also.

7. Spring context module

Spring context is nothing but a configuration file written in XML. It gives the context information like dependency injection information, services required to run the application like validation, internationalization and JNDI etc. to the Spring framework.

Spring Containers (BeanFactory vs ApplicationContext)

Spring comes with two container interfaces – BeanFactory and ApplicationContext.

1) BeanFactory interface and its sub class XmlBeanFactory.

2) ApplicationContext interface and its sub classes FileSystemXmlApplicationContext, ClassPathXmlApplicationContext and XmlWebApplicationContext.

BeanFactory is a Lazy Bean Instantiation container.

ApplicationContext is Eager Bean Instantiation container.

In both the containers, the beans are singleton by default.

In case of BeanFactory until the getBean() method is called bean instances are not created.

In case of ApplicationContext during container start up itself all bean instances are created except prototype bean. Prototype beans are instantiated during getBean() method call.

image

1. BeeanFactory and XMLBeanFactory are used for standalone applications (running from some GUI like Swing GUI on client-side). These come with lazy bean instantiation container. When the client program is executed only, then bean objects are created at runtime by reading applicationContext.xml file.

2. On the contrary, ApplicationContext and its three subclasses come with eager bean instantiation container. At the server start up, applicationContext.xml file is read and objects are created. These classes are used for application servers like Web application and enterprise applications servers.

Note: Separate program (by name, BeanFactory-Singleton-Prototype) available showing clearly the difference between both the two interfaces and also singleton and prototype.

A similar explanation on Hibernate introduction is available at Hibernate Tutorial – JDBC vs Hibernate

Leave a Comment

Your email address will not be published.