Spring Android and Maven (Part 2)

Engineering | Roy Clarkson | February 09, 2011 | ...

In Spring Android and Maven (Part 1), I described how to build an Android application from the command-line using Maven. In this post, I will show you how to build an Android application with Maven dependency management from the Eclipse IDE. The application will also showcase the latest features in Spring Android 1.0.0.M2, which was released this week.

Overview

The Maven Android Plugin lets you build your Android applications with Maven and benefit from dependency management. Google's Android Development Tools (ADT) plugin allows you to develop and build Android applications within the Eclipse IDE. To get Maven dependency management within Eclipse, the Maven Integration for Android Development Tools plugin is required, which integrates m2eclipse, the ADT Plugin, and the Maven Android Plugin

This week in Spring: February 8th, 2011

Engineering | Josh Long | February 08, 2011 | ...

This has been another exciting week in the Spring community. The community seems abuzz about the new Tomcat release, mobile clients, Spring Data and big data (e.g., Spring Gemfire, Redis, and of course the Spring Data projects.)

A reminder: Juergen Hoeller - lead of the core Spring framework and contributor to all of the sister Spring projects, in some fashion or another - is giving two identical webinars - one for North America and one for Europe - on the new features in Spring 3.1 in two days (February 10th)! Be sure to register for this free webinar on the next iteration of the most widely used Java framework!

This is turning out to be a record event, with incredible advance registration numbers. While there are no Spring 3.1 binaries (yet; stay tuned!), the code for the Spring project is always at your disposal, so - if you're as excited about all the new features as I am - you'll see this webinar and start playing with it long before it's…

This week in Spring: February 1st, 2011

Engineering | Josh Long | February 02, 2011 | ...

This week's been a fascinating rush of developments in the community. Depending on where you look, the excitement is behind the newer, more cutting edge stuff like Spring Data, Gemfire, and Virgo, or it's behind the wonderful refinements to technologies like Tomcat and SpringSource Tool Suite. Either way, there's a lot to take in this week, so let's get started!

  1. SpringSource Tool Suite 2.6.0.M1 has been released. This new version features improved content-assist and quick fixes, new wizards and views for Spring MVC @RequestMapping-based applications. It also features improved content-assist performance for Groovy, and improved syntax highlighting for Groovy.
  2. Spring Data Graph - Neo4j Support 1.0.0.M2 Released. The new release has many new features, including improved indexing support - full, named index support for nodes and relationships, removal of node and relationship entities, strict type checking on entity instantiation from framework methods (according to type strategy), support for dynamic projection to arbitrary graph entities, and new updated support for Neo4j 1.2.
  3. Shekhar Gulati has posted an introduction to Spring Roo (part 1, building from source) on IBM's DeveloperWorks.
  4. Mark Thomas, a senior contributor to the Tomcat project, has written about preventing Cross-Site Scripting attacks in Tomcat 7
  5. The VMWare vFabric GemFire team has just put together a video demonstrating how to setup the GemFire's Hibernate Cache Module. The video demonstrates how to configure the module in Maven and then demonstrates its use in a Spring-DAO based implementation. Finally, the use of Spring Insight to monitor the application's performance is demonstrated. Cool video for an even cooler technology!
  6. Sebastian Pietrowski has published a good introduction to Spring Data Redis.
  7. Shekhar Gulati contributed another great post - also on Spring Data and Redis. Spring Data is the umbrella name for a slew of technologies designed to support more specialized data persistence needs. A large part of this is the deep support for many of the NoSQL data stores available today, as well as more specialized support for JDBC-based persistence.
  8. The Java Code Geeks have written up a good post on Aspect Oriented Programming with Spring AOP
  9. Jose Delgado wrote up a good post on how to configure an application to handle two databases in Spring Roo.
  10. Sivaprasadreddy Katamreddy is at it again, this time on applying Inversion of Control and Dependency Injection to method design
  11. Glyn Normington tweets that the Eclipse Virgo kernel is now running successfully for the first time on a directed graph of regions implemented using OSGi 4.3 framework hooks. Congratulations! Eclipse Virgo is the name of the new-and-improved version of what was formerly the SpringSource dm Server.
  12. David Dossot retweeted an interesting post that explores

    the Activiti BPMN 2 engine using Spring from earlier last month.

  13. With all the exciting news of Tomcat 6 and 7, it's easy to forget that Tomcat 5 is still being updated and supported. Tomcat 5.5.32 has also just been released!

Green Beans: Getting Started with Enterprise Messaging and Spring

Engineering | Josh Long | January 26, 2011 | ...

In this post, we will introduce the core concepts of messaging, as well as the rich support for various types of messaging that the Spring framework and its sister projects provide.

What is Messaging? To best explain this, I'll paraphrase the example offered by the groundbreaking Enterprise Integration Patterns book by Gregor Hohpe and Bobby Woolf (Addison Wesley, 2004). When you make a telephone call, you attempt to relay information to another party. This only works if the second party is available when you place the phone call. Because it is not always possible to answer phone calls, we use a voice-mail boxes to queue the messages. Callers leave messages in the voice-mail box and the callee is then free to retrieve the message (or, indeed, many messages) at a later point, asynchronously.

In that example, a voice-mail box sits in the middle of the two parties. It stores the message and then delivers it when the callee – the recipient – retrieves it. In the world of enterprise messaging, things work very much the same: a party sends a message to a messaging broker (also known as messaging-oriented middle-ware – MOM) and another party – when that party can – takes delivery of, or explicitly queries for, any messages from the message broker.

Here is where the analogy stops being useful. Message brokers, in contrast to voice-mail boxes, have a lot of options. Message brokers are ideally positioned to provide extra services, like routing, and to make guarantees about message delivery. Message brokers can be optimized for different use cases, for example, you can trade speed for durability. Message brokers may persist messages to an external store to ensure durability, though this is typically a configuration that can be toggled in the name of speed.

In the voice-mail box example, a message was sent by one party and then delivered to another – the communication was point-to-point. Message brokers support this, as well as another type of communication called publish-subscribe, where messages are delivered to multiple clients.

A common use of message brokers is to solve integration problems between two different systems. Data sent to a message broker is usually of a format common to both the sender and recipient of the message. The only thing that two systems need to agree on to use a message broker is the data contract. Messages typically have a message body, in which the contents of the message itself are stored, and message headers, which are key / value pairs that provide meta-data about the body of the message that can be used to aid consumers of the messages in processing the message. Message headers can be anything you like, but they typically relate to the message itself, or to the processor of the message.

Java Message Service

The Java Message Service (JMS) API specifies client interfaces for interacting with message brokers. Each message broker provides its own implementation of the API, very much like JDBC drivers do for the JDBC API. This implies that JMS clients should generally use the same version of the client as the server. There are many, many fine JMS broker implementations to choose from. One reason for this is that messaging ihas always been an important part of application development, and continues to be even more so today. JMS has been a part of the J2EE (now Java EE) specifications since 1.1. The JMS specification has been at version 1.1 for most of the last decade.

In JMS, clients use a javax.jms.ConnectionFactory to create a javax.jms.Connection. The Connection can then be used to create a javax.jms.Session. The Session represents the client interaction with the broker and allows for sending and receiving messages as well as other less obvious operations.

The most useful methods on the interface concern the creation of a message producers, and message consumers that send and receive messages to and from a javax.jms.Destination. A Destination maps the JMS concept of an "address" on a message broker. It also maps the concept of where a broker stores messages. In JMS, messages are sent to, stored in, and consumed from the same place, all represented by a javax.jms.Destination instance.

[caption id="attachment_7506" align="alignnone" width="573" caption="Above, blue elements represent producers and consumers. The orange elements represent destinations in the broker where messages are buffered. In JMS, these are either topics or queues."][/caption]

Destination is an interface and has two more specific sub-interfaces, javax.jms.Queue and javax.jms.Topic. A Queue represents a standard queue, which is a point-to-point construct as described before. A Topic provides publish-subscribe messaging and can deliver a single message to multiple recipients.

To send a message to a Destination, you must create a javax.jms.MessageProducer. The MessageProducer can then be used to send javax.jms.Messages.

JMS supports two different mechanisms to receive messages. The first way is to ask for a message, using the javax.jmx.MessageConsumer#receive() method, which returns an individual message from a Destination in a synchronous manner; the method blocks until a message is received, by default. Instead of using a MessageConsumer, clients may install a javax.jms.MessageListener by calling javax.jms.Session#setMessageListener(MessageListener). MessageListener is an interface and has only one method, public void onMessage(javax.jms.Message), which will be called whenever a javax.jms.Message is available for consumption on a Destination. A MessageListener provides asynchronous message processing: as messages arrive, they are processed.

There is quite a bit more to learn in the JMS API, but these classes and concepts will help you most in our discussion of Spring's support for JMS messaging. The first level of support is the org.springframework.jms.core.JmsTemplate, which provides simplifying methods to reduce the things we just discussed to one-liners. The JmsTemplate, requires a javax.jms.ConnectionFactory instance to do its work. JmsTemplate can do a lot of work for you. For example, to send a message, the JmsTemplate establishes a javax.jms.Session, sets up a javax.jms.MessageConsumer or javax.jms.MessageProducer, sets up all the machinery for transactions, and provide you with a reference to the current javax.jms.Session so you can create the message of your choice and send it. With all the error handling and construction logic, that's easily a savings of dozens of lines of code. Once your message has been sent, it destroys or closes most of those objects. This is standard practice in application servers (like a Java EE server) because the ConnectionFactory instances are created by the server, managed by the server, and are pooled. They cache the instances after use. Closing resources in those environments simply returns them to a pool. So, the JmsTemplate does the right thing in the standard case, assuming the ConnectionFactory caches or pools instances.

In a managed environment like an application server, you will typically need to acquire the javax.jms.ConnectionFactory from JNDI. You can use Spring to lookup that reference for you and configure a JmsTemplate. In our examples, we want to operate more loosely, so we will use the standalone ActiveMQ message broker. ActiveMQ is a popular, open-source message broker. To use it, download it, and then run the startup script appropriate to your operating system in the bin folder. In your application, you'll need the client libraries to connect for the corresponding version of ActiveMQ. At the time of this writing, the latest version of ActiveMQ was 5.4.2. If you are using Maven, add the following dependencies to your Maven pom file:



            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-core</artifactId>
                <version>${activemq.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-optional</artifactId>
                <version>${activemq.version}</version>
            </dependency>

Be sure to either create a Maven property for ${activemq.version} or replace the string manually with the appropriate version. There is an activemq-all dependency out there as well, but it brings down a lot of perhaps unnecessary jars. For our application, the two dependencies above suffice.

Using Spring with JMS

Let's examine the configuration for a basic JMS application. First, let's examine the basic Spring XML configuration:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/200…

This week in Spring: January 18th, 2011

Engineering | Josh Long | January 18, 2011 | ...

Another fantastic week for the Spring community at large. In addition to the exciting announcement of the stable release of Tomcat 7, there were numerous great posts from the community.

  1. Continuing the back-to-basics "Green Beans" series, we've just published a post on using Spring and Maven together.
  2. The Tomcat Expert community has announced the first stable build of Tomcat 7. We know Tomcat is the favorite place for Spring developers to run their applications, so be sure to check out the latest features.
  3. Nicholas Sankel has written up a blog on using Spring Roo with the Vaadin UI framework.
  4. The Tomcat Expert community has provided a list of the top 10 articles from 2010.
  5. Tomasz Nurkiewicz has written a fantastic blog exploring how you can completely strip out XML using the Spring framework.
  6. Sivaprasadreddy Katamreddy has written up an introduction to Spring AOP that progresses from problematic code all the way to the improved code using Spring AOP.
  7. A gentleman named (from what I can tell) Warlock has blogged on Spring MVC's annotation-based session-handling support.
  8. Sal Peace has written up a fantastic introduction to using Spring MVC 3.0's portlet support with WebSphere 7.
  9. Viral Patel has written up a tutorial on saving and retrieving BLOB object data in Spring 3 MVC and Hibernate
  10. Jorram Barrez (from the Activiti project) has posted on using Spring to connect an Activiti BPMN 2.0 business process to EJB business services.

Green Beans: Getting Started with Maven and Spring

Engineering | Josh Long | January 17, 2011 | ...

Apache Maven is a popular open source tool that offers a convention-over-configuration approach to project build management. Indeed the Eclipse Community Surveys show Maven increased its adoption from 8% in 2009 to 28% in 2010, underscoring its usefulness in a wide range of project settings. Even though you can use Spring without using Maven, there are many reasons to recommend its use to Spring developers. In this post I'll show you how to get started using Maven, and how to use it successfully with Spring libraries, repositories and tooling like SpringSource Tool Suite and Spring Roo.

Maven handles project builds. Maven can provide a lot of power and sophistication with relative ease if your project follows Maven's conventions. It is declarative; you describe what you want done, not how you want it done. This approach will seem different if you've come from venerable build tools like Make, or Ant.

You declare your project's dependencies in the Maven project configuration. These dependencies are then resolved on your behalf and downloaded for you. This is similar to the package system found in many different operating systems. Suppose you're using OS X's fink or ports command line tools. To update an operating system's a functionality, a user selects a package (say, the latest security patches, or a new version of the glib library) to install in an administration client and then instruct the client to downloaded and install it from well-known servers, referred to as package repositories. When the package is downloaded, the package manager consults the package's manifest which enumerates all the libraries (found in other packages) that the package depends on - its transitive dependencies. Those too are downloaded.

There are several ways to install Maven If you haven't already got it installed. Download it from the Apache web site. Choose a more recent version. Currently, many people are using Maven 2.21, or the recently released Maven 3. Download the version you'd like to use and then unzip it to a directory of your choice. Alternatively, several operating systems provide Maven 2 builds (and soon, Maven 3 builds) in the package system. For example, on Ubuntu, you can run sudo apt-get install maven2 . If you're using the SpringSource Tool Suite (available here for download for free), then you don't need to worry, Maven's already downloaded and included in your STS installation folder. Regardless of how you get the Maven binaries on your system, ensure that the binary is on your operating system's search path. Usually, this is just a matter of adding the Maven installation's bin folder to your operating system's PATH variable. It's also a good practice to create a system variable for the Maven installation itself, called MAVEN_HOME. On a Unix (including OS X) or Linux machine, this setup looks about the same. On my machine (an Ubuntu Linux machine), it looks like this:

export MAVEN_HOME=/home/jlong/bin/springsource/maven-2.2.1.RELEASE 
export PATH=$PATH:$MAVEN_HOME/bin

To test it, open up a new shell and issue the following command:

mvn --version

You should see some output confirming the command presence on your system, like this:


jlong@jlong-mbp:~/Desktop/code$ mvn --version 
Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700) 
Java version: 1.6.0_22 
Java home: /usr/lib/jvm/java-6-sun-1.6.0.22/jre 
Default locale: en_US, platform encoding: UTF-8 
OS name: "linux" version: "2.6.35-22-generic" arch: "amd64" Family: "unix" 
jlong@jlong-mbp:~/Desktop/code$ 

Getting Started With Maven

Maven projects assume a standard directory structure, which looks - at a minimum - like this:

./pom.xml ./src ./src/main ./src/main/java ./src/main/resources ./src/test ./src/test/java ./src/test/resources

At the root of the directory structure is a XML file (always called pom.xml) that Maven expects. The pom.xml (POM is short for Project Object Model) describes the things specific to your project that can't be inferred automatically like dependencies, the name of the project, etc.

Directory Description Directory's Contents (relative to the project root)
src/main/java Contains the Java source code for your project
src/main/resources Contains any classpath-relative resources for your project (like, a Spring application context .xml file)
src/test/java Contains the java source code for your test…

What's New In Spring Roo 1.1.1

Engineering | Ben Alex | January 11, 2011 | ...

It's our pleasure to announce the general availability of Spring Roo 1.1.1. This new release incorporates over 250 fixes and enhancements, including many performance and functionality improvements.

As always, SpringSource Tool Suite (STS) 2.5.2 will be released very shortly and incorporate this new version of Spring Roo. You can also download the standalone version of Spring Roo 1.1.1 and get started immediately.

Let's briefly tour some of the more notable enhancements we're added to Roo 1.1.1. If you're upgrading your Roo-based projects, we encourage you to review the version-specific upgrade notes we include in the reference guide. The reference guide is also included in the…

Spring Web Services 2.0 Released

Engineering | Arjen Poutsma | January 11, 2011 | ...

After being in the works for almost a year, I'm happy to announce that Spring Web Services 2.0 has been released! In this post, I'd like to go over some of the major new features.

Java 5+ and Spring 3.0 Required

As you are probably aware, we moved the Object XML Mapping (OXM) module from the Spring-WS project into Spring 3.0. As such, it was a bit problematic to use Spring-WS 1.5 (with its own OXM module) with Spring 3.0, due to conflicting classes in the org.springframework.oxm package.

As of version 2.0, we no longer ship the OXM module as part of Spring-WS, but depend on Spring's OXM instead. As a result, Spring Web Services 2.0 requires Spring 3.0 to work. Normally, we tend to be a bit more lenient with regard to version requirements, not necessarily requiring the latest Spring version, but this was the…

Green Beans: Getting Started with Spring in your Service Tier

Engineering | Josh Long | January 08, 2011 | ...

All applications stem from a domain model. The term "domain model" describes the nouns, or data, in a system that is important to the problem you're trying to solve. The service tier - where business logic lives - manipulates the application data and must ultimately persist it (typically, in a database). The explanation is simple, but in practice building a good service tier can be a daunting task for any developer. This post will introduce developers to the options available in the Spring framework for building a better service tier. It is assumed that the reader has some experience with the…

Green Beans: Getting Started with Spring MVC

Engineering | Colin Sampaleanu | January 04, 2011 | ...

Spring MVC, a part of the core Spring Framework, is a mature and capable action-response style web framework, with a wide range of capabilities and options aimed at handling a variety of UI-focused and non-UI-focused web tier use cases. All this can potentially be overwhelming to the Spring MVC neophyte. I think it's useful for this audience to show just how little work there is to get a bare Spring MVC application up and running (i.e. consider my example something akin to the world's simplest Spring MVC application), and that's what I'll spend the rest of this article demonstrating.

I'm assuming you are familiar with Java, Spring (basic dependency injection concepts), and the basic Servlet programming model, but do not know Spring MVC. After reading this blog entry, readers may continue learning about Spring MVC by looking at Keith Donald's Spring MVC 3 Showcase

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all