Spring Provides a Light-Weight Solution for Building Enterprise Ready Applications
By: Etisbew | Posted: 09th September 2010
Spring provides a light-weight solution for building enterprise-ready applications, while still supporting the possibility of using declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database. Spring provides an MVC framework, transparent ways of integrating AOP into your software and a well-structured exception hierarchy including automatic mapping from proprietary exception hierarchies.
Spring could potentially be a one-stop shop for all your enterprise applications however, Spring is modular, allowing you to use parts of it, without having to bring in the rest
Spring Core
The core module is essentially the foundation for Spring. It provides fundamental features such as dependency injection and management of beans. Some of the top-level packages that fit under this module are org.springframework.beans, org.springframework.core, and org.springframework.util.
Spring Context
The context module is perhaps the second most important module in Spring, after the Spring Core module. It contains key classes such as ApplicationContext and WebApplicationContext, In addition, we will use the org.springframework.mail (for sending emails) and org.springframework.validation (for validating web UI fields) packages, which are also considered part of this module.
Spring AOP
We will not write AOP code directly using Spring. However, indirectly we will end up using Spring AOP-based facilities, such as interceptors for our web application and declarative transaction management. In fact, the Spring reference documentation mentions the same point; that is, if the prepackaged functionality provided in Spring is sufficient for your needs, you do not need to use Spring AOP (to write custom aspects, for example).
Spring DAO
Because we are already using Hibernate for database persistence, we do not need this module. However, this module is worth checking out if you like, or need, to work with JDBC but don't like all the tedious try-catch-finally blocks, opening/closing connections, and more. In addition, Spring goes one step further by providing a consistent exception hierarchy, which can convert vendor-specific checked exceptions into more consistent runtime exceptions that can be caught only in the layer of your application you want and ignore it in other places (thereby eliminating the need for cumbersome try/catch/finally code blocks).
Spring ORM
Spring's Object-Relational Map (ORM) module provides integration support for popular ORM products used by Java developers, such as Hibernate, JDO, Oracle TopLink, Apache OJB, and iBATIS SQL Maps.
Some of the benefits of using Spring's ORM support include ease of testing (via dependency injection), common data exceptions, persistent resource management (for example, Hibernate's SessionFactory), integrated enterprise-class transaction management, and more.
Benefits of Using Spring
• POJOs Spring enables to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server if your application doesn't require all the capabilities that such products provide. With Spring, you have the option of using only a robust servlet container such as Tomcat or some commercial product.
• Modular Spring is organized in a modular fashion. Even though the number of packages and classes is substantial, you have to worry only about ones you need. Hence, phasing Spring into an existing or new project can be done on a case-by-case and module-by-module basis.
• Complementary Spring does not reinvent the wheel; instead, it truly complements some of the existing work out there. For example, it complements several ORM frameworks, JEE, Quartz and JDK timers, other view technologies, and more.
• Testing Testing an application written with Spring is simple because environment-dependent code is moved into this framework (versus JNDI lookups embedded in code, for example). Furthermore, by using JavaBean-style POJOs, it becomes easier to use dependency injection for injecting test data (perhaps by using an XML file as the source of test data). In addition, Spring's mock classes can help you simulate classes such as an HTTP request object. This is primarily true because dependency injection works with setter and getter methods. Hence it is easy to inject test data into your objects and unit test your code using a product such as JUnit. This also includes testing of web components developed using Spring's web MVC framework, as you will see in the next chapter.
• Singletons Spring eliminates the need to maintain your own singleton classes. Instead, you write a class as a normal POJO (without the need for static variables/methods), and Spring ensures that you always get access to the same object, unless you override the default by defining a class as non singleton.
• Web framework 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. It enables you to develop no-form screens, simple-form screens, wizard like screens, and much more. It can also bind HTML form fields directly to the business objects instead of having to write custom classes that extend the web framework's classes. Spring Web MVC is also view agnostic, so it can work with Java Server Pages (JSP), Velocity, Java Server Faces (JSF), and others. In addition, the Spring Web Flow subproject can be used to develop web applications that require state management across several HTTP requests (an online airline-booking website, for example).
• Consistent exception hierarchy Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions (org.springframework.dao. PessimisticLockingFailureException, for example).
• Enterprise-class transaction management Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example). Spring also provides application server-specific integration, for instance, with BEA's Web Logic Server and IBM's WebSphere. Spring's transaction management can be used programmatically or declaratively.
• Lightweight container IoC containers tend to be lightweight, especially when compared to EJB containers, for example. This can be beneficial, perhaps for developing and deploying applications on computers with limited memory and CPU resources.
Dependency Injection:
Dependency injection is a style of IoC; another style uses a template/callback technique. In both cases you are giving control to something else (hence, the term inversion of control). We will, of course, use the dependency injection style.
What is dependency injection exactly? Let's look at these two words separately. First, there is the dependency part; this basically translates into an association between two classes. For example, class A might need class B to get its job done. In other words, class A is dependent on class B. Now, let's look at the second part, injection. All this means is that class B will get injected into class A by the IoC container. This injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods.
In other words, Class A depends on Class B hence, we will use Spring to automatically create a single instance of the Class B and inject (set) it via the ClassA. setClassB method.
Injection Styles
There are two key types of dependency injection styles: one via arguments passed to the constructor when an object is created and the other via the setter methods of a Java Bean style class, after the object has been created.
Spring advocates the use of setter-based dependency injection, because passing many arguments to the constructor can get cumbersome.
Although there are pros and cons to both approaches, considering that we are using Spring, we will go with Spring's recommendation and accordingly use setter-based injection.
a third type, interface injection.
Beans, BeanFactory, and ApplicationContext
The org.springframework.beans.factory.BeanFactory interface is the actual IoC container! It is an interface that essentially manages the application's configuration by instantiating and managing beans defined via code or, in our case, XML files. Beans can be any type of objects, but commonly are JavaBean style classes that is, classes with getters and setters.
The org.springframework.context.ApplicationContext essentially extends BeanFactory and adds additional facilities such as resource bundles, integration with Spring AOP, message resource handling, event broadcasting, and more. Furthermore, WebApplicationContext extends ApplicationContext by adding web application-specific context. Although you can use an implementation of the BeanFactory interface (for example, XmlBeanFactory), it is recommended that you use ApplicationContext for server-side applications.
The following list describes a couple of additional notes about beans:
• Beans can be created by Spring via the constructor (as you normally would using a new statement) or via static factory methods of another class.
• Each bean must have a unique id (for example, springtestmessage).
• Beans can be of two types, Singleton and Prototype (nonsingleton). By default, all beans are singletons, but you can make them prototypes by specifying an attribute such as singleton="false". We will use singletons most of the time because Spring cannot manage the life cycle of a prototype bean after it has been created and handed off to (injected in) the objects that depend on it. Also, by specifying singleton="false" for a given bean definition, you are instructing Spring to create a new instance of the bean every time there is a request for this bean. Last, almost 100% of the time, using singleton beans is sufficient.
• You can inject values in a setter method or constructor as single objects or as collection elements (for example, List, Set, Map, and Properties).
The spring-beans.xsd (or the older spring-beans.dtd) file provides the XML schema for the Spring Framework's application context file.
Etisbew an Offshore Outsourcing Java Software Development Company and has an impressive track record and expertise in the latest spring hibernate, spring mvc, spring aop, spring webflow, spring net, spring security, spring hibernate struts, spring batch, Healthcare Software Solutions, Internet Marketing Techniques, Internet advertising techniques, Information Technology, Website Redesign, Custom Application Development, ColdFusion, Dotnet, Php, Java and Search Engine Optimization.This article is free for republishing
Printed From: http://www.goinglegal.com/spring-provides-a-lightweight-solution-for-building-enterprise-ready-applications-1739046.html
Back to the original article
Tags: mail, hierarchy, persistence, sending emails, web application, remote access, validation, hierarchies, fundamental features