Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

Spring and Java

The Spring Framework is a leading full-stack Java/J2EE application framework. It is aimed primarily at Java projects and delivers significant benefits such as reducing development effort and costs while providing facilities to improve test coverage and quality.

Let's have a look at Spring in action for a Java application which prints out information about a country.

Suppose we have the following interface:

And the following implementation class:

Spring supports the Dependency Injection style of coding. This style encourages classes which depend on classes like USA not to hard-code that dependency, e.g. no fragments of code like 'new USA(...)' and no 'import ...USA;'. Such a style allows us to change the concrete implementations we depend on for testing purposes or at a future point in time as our program evolves. In our example above, we might declaratively state our dependencies in a beans.xml file as follows:

In this example, the <constructor-arg/> element allows us to use constructor-based injection.

Having done this, we can get access to our beans through a variety of mechanisms. Normally, access to the beans is mostly transparent. In our case though, we are going to make access explicit so you can see what is going on. Our main method might look like this:

Running this results in:

Spring and Groovy

We can extend this example and introduce Groovy in a number of ways. Firstly, we can create the following Groovy class:

And this one too:

So long as the first class is available on the classpath in compiled form, we can simply reference this class in our beans.xml file as follows:

Alternatively, if the source file will be on the classpath, we can use:

In these examples, the <property/> and <lang:property/> elements allows us to use setter-based injection.

Spring and Ruby

If we prefer to code in another language (with a few restrictions - see below), Spring supports other languages too, e.g.:

Spring and Groovy Again

But wait there's more ...

Suppose now that we wish to sort our countries according to population size. We don't want to use Java's built-in sort mechanisms as some of them rely on our objects implementing the Comparable interface and we don't want that noise in our Ruby script. Instead we will use Groovy. We could simply write a Sort class in Groovy and reference as we have done above. This time however we are going to use an additional Spring feature and have the scripting code within our beans.xml file. First we define the following interface:

We can then include the Groovy sort code directly into the beans.xml file as follows:

Putting it all together

We now combine all of the approaches above in a final example.

Here is the complete beans.xml file:

Our Java code to use this example looks like:

And the resulting output (with a little bit of hand formatting) looks like:

What we didn't tell you yet

  • Spring supports BeanShell in addition to JRuby and Groovy
  • Spring supports the concept of refreshable beans when using the <lang:language/> element so that if your bean source code changes, the bean will be reloaded (see GINA or the Spring doco for more details)
  • The Groovy scripting examples in the current Spring documentation are based on an old version of Groovy, ignore the @Property keywords and use the latest groovy-all-xxx.jar instead of the jars they recommend
  • The <lang:language/> element currently only supports setter-based injection

Current Limitations

Currently using the Groovy language through Spring is extensively supported. Using other languages like Ruby takes a little more care. Try restricting your Ruby methods to ones which take and return simple data types, e.g. long, String and you may find that certain operations don't work as you'd expect when working between Ruby and other languages.

Further Information

  • No labels