Java Spring切入点指示符差异(在与执行内)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20816780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring pointcut designators differences (within vs execution)
提问by glazaror
Please... can anybody explain me what are the differences between using the following spring pointcut designators?
请......有人可以解释我使用以下弹簧切入点指示符有什么区别吗?
Using "within pointcut designator":
使用“内切入点指示符”:
<aop:pointcut expression="within(my.app.dao.impl.*)" id="commonDaoOperation"/>
Using "execution pointcut designator":
使用“执行切入点指示符”:
<aop:pointcut expression="execution(public * my.app.dao.impl.*.*(..))" id="commonDaoOperation"/>
I am using the second one in my web-projects (and I think it's the most used), the problem i have found with this approach is that it's consuming a lot of memory in the heap...
我在我的网络项目中使用第二个(我认为它是最常用的),我发现这种方法的问题是它在堆中消耗了大量内存......
After analyzing the "heap dump" of the application server with the "eclipse memory analyzer" i have found that my application is consuming 450 MB and the instances of the class "org.springframework.aop.aspectj.AspectJExpressionPointcut
" are consuming 30% of those 450MB...
在使用“eclipse 内存分析器”分析应用服务器的“堆转储”后,我发现我的应用程序消耗了 450 MB,而“类"org.springframework.aop.aspectj.AspectJExpressionPointcut
”的实例消耗了这 450 MB 中的30%...
Each instance of AspectJExpressionPointcut
occupy 6 MB (approximately) and this is because each instance mantains a cache of matches with instances of java.lang.reflect.Method and surprisingly there are a lot of java methods cached (methods that my pointcut expression doesnt mentions).
每个实例AspectJExpressionPointcut
占用 6 MB(大约),这是因为每个实例都维护一个与 java.lang.reflect.Method 实例匹配的缓存,并且令人惊讶的是缓存了很多 java 方法(我的切入点表达式没有提到的方法)。
After Reading Spring Documentation, I decided to use the first one approach (within pointcut designator) and now each instance of AspectJExpressionPointcut
occupy much less memory.
阅读 Spring 文档后,我决定使用第一种方法(在切入点指示符内),现在每个实例AspectJExpressionPointcut
占用的内存要少得多。
The question is about that... what is the difference in performance between them...
问题是……它们之间的性能有什么区别……
Many thanks in advance...
提前谢谢了...
回答by Sotirios Delimanolis
The Spring documentationexplains the difference:
在Spring文档解释了区别:
- execution- for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP
- within- limits matching to join points within certain types (simply the execution of a method declared within a matching type when using Spring AOP)
- execution- 用于匹配方法执行连接点,这是您在使用 Spring AOP 时将使用的主要切入点指示符
- 范围内- 限制匹配某些类型内的连接点(简单地执行在使用 Spring AOP 时在匹配类型中声明的方法)
In other words, execution
matches a method and within
matches a type.
换句话说,execution
匹配一个方法并within
匹配一个类型。
In this case, your pointcuts are pretty much equivalent. Your within
matches any type in the package my.app.dao.impl
and your execution
matches all the public methods of any type in the package my.app.dao.impl
.
在这种情况下,您的切入点几乎是等效的。你within
匹配任何类型的包my.app.dao.impl
和你execution
匹配任何类型的包的所有公共方法my.app.dao.impl
。
However, execution
is implemented, I think, with an interceptor for each matched method (a lot of objects), which within
only needs one interceptor since it matches the entire type (very little objects).
但是,execution
我认为,为每个匹配的方法(很多对象)使用了within
一个拦截器,它只需要一个拦截器,因为它匹配整个类型(非常少的对象)。
回答by Sabina Orazem
execution() matches join points that are method executions. This is the only designator that actually performs matches. All other designators (supported by Spring AOP) only limit those matches. Note that Spring supports only a subset of designators available in AspectJ (Spring AOP is proxy-based). AspectJ pointcut designators that are supported in Spring are: args() and @args(), target() and @target(), within() and @within(), execution(), this(), @annotation
execution() 匹配方法执行的连接点。这是实际执行匹配的唯一指示符。所有其他指示符(由 Spring AOP 支持)仅限制这些匹配。请注意,Spring 仅支持 AspectJ 中可用的指示符子集(Spring AOP 是基于代理的)。Spring支持的AspectJ切入点指示符有:args()和@args()、target()和@target()、inside()和@within()、execution()、this()、@annotation