gpt4 book ai didi

java - Spring AOP 表达式中访问修饰符有什么用?

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:07 25 4
gpt4 key购买 nike

来自Spring doc :

6.2.3.4. Examples

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression is:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

我可以看到修饰符模式吗?你可以在哪里说公共(public)的、私有(private)的、 protected 。在同一份文件上它说:

6.2.3.1. Supported Pointcut Designators

Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

我有点困惑,使用修饰符模式有什么意义?请举个例子?

最佳答案

该文档现已过时。最新的是https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop-pointcuts-designators并说

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are, by definition, not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy are intercepted (and even package-visible methods, if necessary). However, common interactions through proxies should always be designed through public signatures.

Note that pointcut definitions are generally matched against any intercepted method. If a pointcut is strictly meant to be public-only, even in a CGLIB proxy scenario with potential non-public interactions through proxies, it needs to be defined accordingly.

If your interception needs include method calls or even constructors within the target class, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving before making a decision.

因此请谨慎使用非公共(public)访问修饰符,但您可以在某些情况下使用 cglib 代理来使用它们。

关于java - Spring AOP 表达式中访问修饰符有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com