Spring framework is a lightweight solution and a potential one-stop platform for building enterprise applications.
Spring is modular, which allows you to use only those modules that you need without introducing unnecessary modules.
You can use the IoC container and use any Web framework on it (for example : Struts, Hibernate and even JDBC abstraction layer).
It also supports declarative transaction management, remote access to your logic through RMI or web services, and options for supporting various data persistence.
It provides a full-featured MVC framework and enables you to transparently integrate AOP into your software.
Spring is designed to be non-invasive, meaning that your business logic code is usually not dependent on the framework itself.
At your integration layer (for example, the data access layer), there will be some dependencies on data access techniques and Spring library files. However, in the rest of your code library, these dependencies are easily isolated.
1. Spring Framework Introduction
Spring framework provides a wide range of infrastructure to support the development of Java applications. It controls the infrastructure itself so that you can focus on your application.
Spring enables you to build applications with simple Java objects (POJOs). And you can apply enterprise services to POJO in a non intrusive way. This function applies to the Java SE programming model, all and part of the Java EE also.
Spring platform has below advantages.
- Execute the database transaction using the Java method without having to refer to transaction API.
- Use the local Java method to execute the remote procedure without involving remote API.
- Perform administrative actions using the local Java method without having to refer to JMX API.
- Use the local Java method for message handling without having to refer to JMS API.
1.1 Dependency Injection And Control Inversion
Java application is a broad term, includes N tier server-side enterprise applications from restricted applets, the entire application is usually made up of multiple objects by collaboration. As a result, objects in an application depend on each other.
Although the Java platform offers rich application development capabilities, it lacks the means to organize its basic building blocks into an organic whole, throwing these tasks only to architects and developers.
The Spring Framework’s Inverse Of Control (IoC) component provides a solution to this problem, which provides an effective method to assemble different components into a fully working application.
The functions contained in the Spring framework are organized into about 20 modules. These modules are divided into Core Container, Web, Data Access/Integration, AOP (Aspect Oriented Programming), Test and Instrumentation, as shown in the following diagram.
1.2.1 Core Container
The core container contains the Core, Beans, Context and SpEL( Spring Expression Language) modules.
The Core and Bean modules provide the foundation for the framework, including the IoC and Dependency Injection capabilities.
BeanFactorys is a factory model for precision implementation. It eliminates the need for Programming single instances, and allows you to remove configuration information and coupling between specific dependencies of the actual program logic.
The Context module builds on the top of Core and Bean modules, it allows you to access objects in the framework, which is similar to the registration of JNDI.
The Context module inherits the features from the Bean module and adds support for internationalization (such as using resource packs), event propagation, resource loading, and transparent creation of contexts (such as Servlet containers).
The context module also supports Java EE features such as EJB, JMX, and basic remote invocation. The ApplicationContext interface is the focus of the context module.
The expression language module provides a powerful expression language for querying and manipulating object graphs at run time. This is an extension of the unified expression language (Unified, EL) in the JSP 2.1 specification.
The expression language supports setting and getting property values, property definitions, method calls, access arrays, collections, and index contexts. Supports logical and numeric operations, naming variables, retrieving objects from the IoC container in Spring by name. It also supports selection operations of list, as well as the common list aggregation operations.
1.2.2 Data Access And Integration
The data access / integration layer includes JDBC, ORM, OXM, JMS, and transaction modules.
JDBC module provide JDBC abstraction layers that do not need to write lengthy JDBC code and parse database vendor specific error codes.
The ORM module provides the integration layer for the popular object relational mapping API, including JPA, JDO, and Hibernate. Using the ORM package, you can use all of the O/R mapping frameworks and combine all of the other features provided by Spring, such as the simple declarative transaction management function mentioned earlier.
The OXM module integrates the abstraction layers that support object /XML mapping implementations, including JAXB, Castor, XMLBeans, JiBX, and XStream.
The Java messaging service (the JMS module) contains the features that generate and process messages.
The transaction module supports programmatic and declarative transaction management for classes that implement specific interfaces and all POJO (common Java objects).
1.2.3 Web Module
The Web layer contains the Web, Web-Servlet, WebSocket, and Web-Portlet modules.
The Spring Web module provides the basic Web oriented integration capabilities, such as multiple file uploads, initialization of IoC containers using the Servlet listener and the Web application context. It also contains the web related parts of the Spring remote access support.
The Web-Servlet module contains the implementation of the model view controller (MVC) model of Spring for Web applications. The MVC framework of Spring provides a clear separation between domain model code and Web forms, and integrates all other features of the Spring framework.
1.2.4 AOP And Instrumentation Infrastructure Components
The Spring AOP module provides AOP alliance compliant aspect oriented programming, which allows you to customize, for example, method interceptors, and cut-in points to completely separate code from each function. Using the metadata function at the source code level, you can also include behavior information in your code to some extent, similar to the .NET attribute.
Individual Aspects modules provide integrated use of AspectJ. The Instrumentation module provides support for class instrumentation and class loader implementation for some application servers.
1.2.5 Test Module
The test module supports the use of JUnit or TestNG to test Spring components. It provides consistent loading of Spring ApplicationContexts, and caching of these contexts. It also provides mock objects that you can use to test your code in isolation.
1.3 Application Scenarios
The modules described earlier makes Spring an ideal choice in many scenarios, from small programs to enterprise applications can all use Spring’s transaction management capabilities and web framework integration.
Spring declarative transaction management capabilities make web applications completely transactional, as if you were using the EJB container to manage transactions.
All custom business logic can be implemented in a simple POJO and managed using the Spring IoC container.
Spring ORM supports integration of JPA, Hibernate, and JDO. For example, when using Hibernate, you can continue using existing mapping files and standard Hibernate SessionFactory configurations.
The form controller seamlessly integrates the Web layer and the domain model bean, eliminating the need for using ActionForms or other classes to pass the values of the HTTP arguments to the domain model bean.
In some cases, you are not allowed to switch to a different framework that you are using. Spring does not force you to use it only, it is not a fully or nothing solution.
Existing front-end frameworks, such as Struts, Tapestry, JSF, or other UI frameworks that allow the use of Spring transaction capabilities, can be integrated with the Spring intermediate layer.
You just need to use ApplicationContext to connect your business logic and integrate your web layer using WebApplicationContext.
When you need to access existing code through web service, you can use Spring’s Hessian-, Burlap-, Rmi- or JaxRpcProxyFactory classes. Enabling remote access to existing applications is not difficult.
Spring Framework provides enterprise Java Bean (EJB ) access and abstraction layer, it can reuse existing POJO and packaging them into a stateless session bean, applied to scalable web application.