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: