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 4 Next »

Boost is a framework for working with Java. This page describes how to use the IoC part of Boost called Spider with Groovy.

Spider has a very interface oriented view of the world. If you are into Interface oriented design (e.g. Ken Pugh's book) and use fine-grained interfaces, you will feel right at home. So first, let's create a Java interface:

and a corresponding implementation:

We could have just as easily used Groovy. As an example, here's another interface (this time Groovy):

And its Groovy implementation:

Now, let's create a class that uses these implementations.

The go() method is the Spider's answer to Java's main() method, i.e. it is the entry point for IoC managed webs of classes. Also, @PackageScope is an AST macro. The Spider does field injection based on package scoped fields. However, Groovy normally turns packages scoped fields into properties and we don't want that here. The AST macro tells the Groovy compiler to leave the fields in the PleaseDontGo class alone and not try to turn them into properties. We could have placed the annotation individually on fields if we preferred. Placing it at the class level makes it apply to all package scoped fields.

Now, let's bootstrap the IoC container so that we can run our application:

Here, BoostWeb provides some nice defaults for us to use. Running this gives:

Doing stuff
Reporting stuff

We can also override the Web. For instance, we can supply our own web:

Which tells the IoC framework to use the supplied closure whenever it needs to inject a MeTwo implementation. Note again the use of the PackageScope AST macro to override Groovy's default property behavior. With this in place, our main script becomes:

And the output will be:

Doing stuff
Reporting stuff2

You can also use Boost's testing facilities:

Here, fooA, fooB and subject will be auto created with default values, foo1Mock and foo2Mock will be auto created as mocks, pa and pb are set using the fixtures() method. GroovyLifecycleTestCase is a slight variation of LifecycleTestCase which is built-in to Boost. The built-in one is a little too opinionated about the methods Groovy adds under the covers to its classes.

  • No labels