Spring

Spring

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.

 Modules

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

Usage scenarios

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 Hessian-Burlap-Rmi- or 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.

 Spring Benefits

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 POJO is that developers do not need an EJB container 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 ORM frameworks, Logging frameworks, JEEQuartz and JDK Timers
  • 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 Struts or other over engineered or less popular web frameworks
  • Spring provides a convenient API to translate technology-specific exceptions (thrown by the JDBCHibernate, or JDO) into consistent and unchecked exceptions
  • Lightweight IoC containers tend to be lightweight, especially when compared to EJB containers. 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 JTA)

It has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>SpringHelloworld</groupId><artifactId>SpringHelloworld</artifactId><version>0.0.1-SNAPSHOT</version></project>

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:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>SpringHelloworld</groupId><artifactId>SpringHelloworld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><dependencies><!-- Spring framework --><dependency><groupId>org.springframework</groupId><artifactId>spring</artifactId><version>2.5.6</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName></build></project>

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:

HelloWorld.java

package com.jcg.spring.example; public class HelloWorld { private String username; public String getUsername() {return username;} public void setUsername(String username) {this.username = username;}}

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:

AppMain.java

package com.jcg.spring.example; import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource; public class AppMain { private static BeanFactory beanFactoryObj; // This Method Is Used To Load The Spring Bean Configuration File And Return The 'BeanFactory' Objectpublic static BeanFactory getBeanFactory() {beanFactoryObj = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));return beanFactoryObj;} // This Is The Main Method And In Here We Are Retrieving The Spring Bean via 'getBean()' Methodpublic static void main(String[] args) {HelloWorld helloObj = (HelloWorld) getBeanFactory().getBean("helloBean");System.out.println(helloObj.getUsername());}}

Once the XML file is created, we will add the following code to it:

spring-beans.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Mapping With Model Class --><bean id="helloBean" class="com.jcg.spring.example.HelloWorld"><property name="username" value="Java Code Geek"></property></bean></beans>

Notes:

  • 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

Executing the 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.


Young Hee Kim

0
0

Laisser un commentaire