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.
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:
BoostWeb provides some nice defaults for us to use. Running this gives:
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:
You can also use Boost's testing facilities. An example (
Person classes not shown) to give you a flavor of these kinds of tests:
subject will be auto created with default values,
foo2Mock will be auto created as mocks,
pb are set using the
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. The result is very compact test code albeit looking a little magical until you get used to this style of testing.