Guys,
I have got it working, somehow, but it involves a workaround which I am not
happy with. First, two things seem to be required in order for advice being
applied to an object in Grails:
1) As has been said before, it must be of the same type that is referenced in
the aspect declaration, otherwise an "instance is not of declaring type" error
is thrown. This can be solved by implementing an interface and match the
pointcut against that interface.
2) The object that is being advised must be a Spring bean, i.e. it must be
managed by the container. This is not true for Grails Controllers, and this
is the reason why I was able to apply advice to my services, but not to my
controllers.
Unfortunately, controllers are instantiated per-request and parameters
bound as such. So what I did was for each controller create an interface
and an implementation of that interface, to which the controller closures
forward, e.g.:
interface Foo {
def foo(def controller)
}
// this must be a managed bean
class FooImpl {
def foo(def controller) {
controller.flash.message = "advice applied!"
}
}
class FooController {
def impl // injected by spring
def fooClosure = {
// forward call
impl.foo(this)
}
}
Now any advice declared on Foo will match, however, this solution has many
drawbacks: Firstly, of course, another level of indirection is introduced,
with all the related drawbacks (performance, readability, ...)
Secondly, all controller logic is now moved to FooImpl, but FooImpl needs
access to "private" controller fields (e.g. params, flash, ...).
Given, right now there is no such thing as private fields or methods in
Groovy, but this may change with upcoming versions I guess and break
this workaround (unless there will be public getters for everything).
Lastly, every controller impl has to be declared manually as a bean.
So, does anyone have a better solution at hand, because I am not very
happy with this.
Thanks and best regards,
Matthias
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email