1. Introduction to Spring Framework
Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
Spring enables you to build applications from “plain old Java objects” (POJOs) and to apply enterprise services non-invasively to POJOs. This capability applies to the Java SE programming model and to full and partial Java EE.
Examples of how you, as an application developer, can use the Spring platform advantage:
- Make a Java method execute in a database transaction without having to deal with transaction APIs.
- Make a local Java method a remote procedure without having to deal with remote APIs.
- Make a local Java method a management operation without having to deal with JMX APIs.
- Make a local Java method a message handler without having to deal with JMS APIs.
The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, and Test, as shown in the following diagram.
Overview of the Spring Framework
The building blocks described previously make Spring a logical choice in many scenarios, from applets to full-fledged enterprise applications that use Spring’s transaction management functionality and web framework integration.
Typical full-fledged Spring web application
Spring’s declarative transaction management features make the web application fully transactional, just as it would be if you used EJB container-managed transactions. All your custom business logic can be implemented with simple POJOs and managed by Spring’s IoC container. Additional services include support for sending email and validation that is independent of the web layer, which lets you choose where to execute validation rules. Spring’s ORM support is integrated with JPA, Hibernate, JDO and iBatis; for example, when using Hibernate, you can continue to use your existing mapping files and standard Hibernate
SessionFactory configuration. Form controllers seamlessly integrate the web-layer with the domain model, removing the need for
ActionForms or other classes that transform HTTP parameters to values for your domain model.
Spring middle-tier using a third-party web framework
Sometimes circumstances do not allow you to completely switch to a different framework. The Spring Framework does not force you to use everything within it; it is not an all-or-nothing solution. Existing front-ends built with WebWork, Struts, Tapestry, or other UI frameworks can be integrated with a Spring-based middle-tier, which allows you to use Spring transaction features. You simply need to wire up your business logic using an
ApplicationContext and use a
WebApplicationContext to integrate your web layer.
Remoting usage scenario
When you need to access existing code through web services, you can use Spring’s
JaxRpcProxyFactory classes. Enabling remote access to existing applications is not difficult.
EJBs – Wrapping existing POJOs
The Spring Framework also provides an access and abstraction layer for Enterprise JavaBeans, enabling you to reuse your existing POJOs and wrap them in stateless session beans for use in scalable, fail-safe web applications that might need declarative security.
In the following list there are some of the great benefits that Spring framework provides:
- Spring enables developers to develop enterprise-class applications using
POJO. The benefit of using only
POJOis that developers do not need an
EJBcontainer product such as an application server but they have the option of using only a robust servlet container such as Tomcat
- Spring is organized in a modular fashion. Even though the number of packages and classes are substantial, developers have to worry only about the ones they need and ignore the rest
- Spring does not reinvent the wheel instead, it truly makes use of some of the existing technologies like
ORMframeworks, Logging frameworks,
- Testing an application written with Spring is simple because the environment-dependent code is moved into this framework. Furthermore, by using JavaBean style, it becomes easier to use Dependency Injection for injecting the test data
- Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as
Strutsor other over engineered or less popular web frameworks
- Spring provides a convenient API to translate technology-specific exceptions (thrown by the
JDO) into consistent and unchecked exceptions
- Lightweight IoC containers tend to be lightweight, especially when compared to
EJBcontainers. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources
- Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database) and scale up to global transactions (using
It has downloaded the maven dependencies and a
pom.xml file will be created. It will have the following code:
The rest dependencies will be automatically resolved by Maven, such as AOP, Spring Core, Spring Beans etc. The updated file will have the following code:
It is a simple Java class with a single attribute plus the corresponding getter and setter method for the same. Add the following code to it:
This class helps in loading the beans configuration using the
BeanFactory object and calls the
getBean() method to get the instance of a bean during the Spring runtime environment. Add the following code to it:
XML file is created, we will add the following code to it:
- Bean Id: A unique id defined for a particular class object or an aspect. Once this is defined, it can be used to access the corresponding class
- Class: Path of the class that is mapped to a particular bean
- Property: This is used to inject values to the attribute using the setter injection
- Scope: Defines the bean scope, as in singleton, prototype etc
AppMain class, developers will see that the value injected as property in the
XML file for the
HelloWorld class gets displayed on the screen. Developers can debug the example and see what happens after every step.