在 Grails 中使用 AOP 不适用于服务 [英] Using AOP in Grails is not working for service
问题描述
我使用 Grails 2.2.3 并在 grails-app/conf/spring/resources.groovy 中键入以下代码
I use Grails 2.2.3 and type following codes in grails-app/conf/spring/resources.groovy
beans = {
xmlns aop:"http://www.springframework.org/schema/aop"
loggerAspect(com.test.aop.aspect.LoggerAspect)
aop{
config("proxy-target-class": true) {
aspect(id: "beforeService", ref: "loggerAspect") {
before method: "beforeMethod",
pointcut: "execution(* com.test.DemoService.serviceMethod())"
}
aspect(id: "afterService", ref: "loggerAspect") {
after method: "afterMethod",
pointcut: "execution(* com.test.DemoService.serviceMethod())"
}
}
}
}
然后,在src/groovy/com/test/aop/aspect下创建一个aspect类
then, create an aspect class under src/groovy/com/test/aop/aspect
package com.test.aop.aspect
class LoggerAspect {
def beforeMethod(JoinPoint jp){
println '-- Before Method.'
}
def afterMethod(JoinPoint jp){
println '-- After Method.'
}
}
并在 grails-app/services/com/test 下创建一个服务类
And create a service class under grails-app/services/com/test
package com.test
class DemoService {
def serviceMethod() {
println 'In DemoService.serviceMethod()'
}
}
并创建一个控制器来调用服务进行测试
And create a controller to call service for testing
package com.test
class DemoController {
def index() {
println 'In DemoController.index()'
def demoService = new DemoService()
demoService.serviceMethod()
render 'Hello World'
}
}
最后通过url测试aop:
Finally, I test the aop through url:
http://myhost:8080/grails-spring-aop/demo/index
并且不调用 aop.结果如下:
and the aop is not invoked. Following is the result:
| Server running. Browse to http://myhost:8080/grails-spring-aop/
In DemoController.index()
In DemoService.serviceMethod()
我将以下行添加到服务类:
I add the following line to the service class:
static transactional = false
而且,它仍然不适合我.
And, it's still not working for me.
任何人都知道如何解决这个问题或者这是不可能的.或者我做错了什么.
Anyone an idea how this can be solved or is this not possible. Or I do the something wrong.
谢谢.
推荐答案
您需要在控制器中注入服务(spring bean)而不是创建它的实例.
You need to inject the service (spring bean) in the controller instead of creating an instance of it.
package com.test
class DemoController {
def demoService //Autowired, not required to specify in resources.groovy
def index() {
println 'In DemoController.index()'
demoService.serviceMethod()
render 'Hello World'
}
}
此外,可以基于以下方式对方面进行注释:
Moreover, the aspect can be made annotation based as below:
package com.test.aop.aspect
@Aspect
class LoggerAspect {
//A more generic advice would be as below
//@Before("execution(* com.test.*.*(..))")
@Before("com.test.DemoService.serviceMethod()")
def beforeMethod(){
println '-- Before Method.'
}
//A more generic advice would be as below
//@Around("execution(* com.test.*.*(..))")
@After("com.test.DemoService.serviceMethod()")
def afterMethod(){
println '-- After Method.'
}
}
而 resources.groovy 可能会变成:
And resources.groovy could become:
beans = {
loggerAspect(com.test.aop.aspect.LoggerAspect)
xmlns aop:"http://www.springframework.org/schema/aop"
aop{
config("proxy-target-class": true) {
aspect(id: "loggerAspectService", ref: "loggerAspect")
}
}
}
这篇关于在 Grails 中使用 AOP 不适用于服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!