October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The New Spring 3.0 Features You Need to Know

  • January 29, 2010
  • By Yuvarani Meiyappan
  • Send Email »
  • More Articles »

The Spring framework is a lightweight, complete solution for building enterprise Java applications. Modularity is its key feature, which allows enterprise application developers to use only the modules they require from the stack. The Spring framework has become popular because it can deliver increased productivity, faster project completion, and improved portability and application quality.

The new Spring framework release, Spring 3.0, is entirely based on Java EE 5 (with backward compatibility for previous versions). In addition to supporting Java 5 features—and offering enhancements to existing APIs, Spring 3.0 also supports some of the new features slated for Java EE 6, such as asynchronous method invocation. The component structure is reorganized from Spring 2.x, and Spring 3.0 introduces some new modules. The Spring 3.0 components are classified as follows:

  • Core Container
  • Data Access/Integration
  • Web
  • Aspect Oriented Programming (AOP)
  • Instrumentation
  • Test

This article examines these notable new features in Spring 3.0:

  • Spring Expression Language (SpEL)
  • Object/XML Mapping (OXM) module
  • Type-conversion system
  • Formatter SPI
  • RESTful web services support

Spring Expression Language

Spring 3.0 introduces Spring Expression Language (SpEL), a powerful expression language that supports querying and manipulating an object graph at runtime. With SpEL, Spring applications can overcome the limitation of using only fixed values in configuration files. SpEL is part of the Spring core container. It can be used for bean definitions in both XML- and annotation-based configurations.

The SpEL syntax to define an expression is #{<expression>}. SpEL supports functions such as literal expressions, regular expressions, relational operators, assignment, boolean and relational operators, and ternary operators. It also supports functions related to classes such as class expression, accessing the properties, invocation of methods, invocation of constructors, and user-defined functions.

While SpEL works for all the products in the Spring portfolio, SpEL is designed to be self-contained and so it can be used independently outside the Spring portfolio as well.

SpEL in Bean Definition

The following is an application context that uses SpEL for bean definition.

  <bean id="customer" class="com.el.Customer">
        <property name="dateOfEnrollment" value="#{new java.util.Date()}" />
    </bean>


Below is a bean class that uses SpEL for bean definition.

public class Customer {
    @Value("#{434}")
    private int customerId;
    private Date dateOfEnrollment;
    …
    @Value("#{new java.util.Date()}")
    public void setDateOfEnrollment(Date dateOfEnrollment) {
        this.dateOfEnrollment = dateOfEnrollment;
    }
}


For the bean class example, the application context should have <context:annotation-config /> .

SpEL in Invocation of Constructor

The following SpEL code snippet instantiates the Customer bean, invoking an overloaded constructor.

ExpressionParser parser = new SpelExpressionParser();
        Customer customer = parser.parseExpression("new 
      com.el.Customer(123,'George')").getValue(Customer.class);


The above code snippet replaces the bean creation by using the ApplicationContext and context.getBean() method. Note that the fully qualified class name should be passed to the parseExpression method.

SpEL in Accessing Member Variable

The following code snippet instantiates the Customer bean and sets the customerId to 124.

        ExpressionParser parser = new SpelExpressionParser();
        Customer customer = parser.parseExpression("new 
      com.el.Customer()").getValue(Customer.class);
 
        StandardEvaluationContext context = new 
      StandardEvaluationContext(customer);
        context.setVariable("cid", "124");   // statement 1
        parser.parseExpression("customerId=#cid").getValue(context);   // statement 2


The statement 1 command sets the cid to 124 within the context. The statement 2 command parses the expression string (customerId=#cid) and evaluates the expression against the context, hence setting the customerId of Customer bean.

As mentioned previously, SpEL supports other functions as well. The above are some of the most interesting.

Object/XML Mapping (OXM) Module

The OXM module of Spring 3.0 (in the Data Access / Integration component) supports the marshalling and unmarshalling of Java objects and XML documents. It also supports various O/X mapping frameworks such as Castor, JAXB, JiBX, XMLBeans, and XStream.

Spring OXM enables rapid application development by allowing easy configuration and injection of marshaller and unmarshaller dependencies. Spring OXM's two global interfaces, Marshaller and Unmarshaller, allow the programmer to switch O/X mapping frameworks with almost no change in the code. Similar to the data access exception hierarchy, the Spring OXM module has an exception hierarchy that wraps the exceptions thrown by third-party providers.

Here is the Customer bean that has to be marshaled/unmarshaled:

public class Customer {
    private int customerId;
    private String customerName;
    // setter and getter method
}


The OXM class in Listing 1 marshals and unmarshals this bean.

The marshal() method of marshaller serializes the customer object to XML, and the unmarshal() method of unmarshaller deserializes the XML to the customer object. The saveCustomer() method serializes the Customer object to CustomerBean.xml, and the openCustomer() method deserializes the XML and returns the customer object.

The marshaller and unmarshaller property of the OXM class is set through the application context, as shown here.

<bean id="oxm" class="com.oxm.OXM" >
        <property name="marshaller" ref="castorMarshaller" />
        <property name="unmarshaller" ref="castorMarshaller" />
    </bean>
 
    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="mappingLocation" value="classpath:mapping.xml" />
    </bean>


In this example, the application context uses the Castor O/X mapping framework for both marshalling and unmarshalling. The Castor XML mapping is shown below (mapping.xml).

<mapping>
    <class name="com.oxm.Customer">
      <map-to xml="customer"/>
      <field name="customerId" type="integer">
         <bind-xml name="customerId" node="element"/>
      </field>
      <field name="customerName" type="string">
         <bind-xml name="customerName" node="element"/>
      </field>
    </class>
</mapping>



Tags: open source, Spring, REST, Java EE, Object/XML Mapping



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel