- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(标题不是最好的,但我找不到一个好的方式来表达以下问题)
给定
@Aspect
@Component
class MyAspect {
@Autowired private MyService service;
@Around("@target(org.springframework.ws.server.endpoint.annotation.Endpoint)")
public Object aroundEndpoint(ProceedingJoinPoint joinPoint) {
return service.around(joinPoint::proceed);
}
@Around("@target(org.springframework.stereotype.Service)") // And some other expressions to exclude `MyService`
public Object aroundService(ProceedingJoinPoint joinPoint) throws Throwable {
// ...
}
}
@Service
class MyService {
// My own Callable<T> with Throwable instead of Exception
public Object around(Callable<?> callable) throws Throwable {
// Do stuff
Object returnValue = callable.call();
// Do stuff
return returnValue;
}
}
当一个端点方法被调用时,它被aroundEndpoint
拦截。如果我立即调用 joinPoint.proceed()
,一切都会按预期进行。但是,如果我将它作为方法引用(或 lambda)传递给 MyService.around
,然后 then 调用它,它会与我的服务切入点和我的 around 服务相匹配建议应用于它。
我做了一些调试,这是我看到的:在 AspectJExpressionPointcut.matches
中,thisObject
和 targetObject
指的是我在前者中的端点情况下,但在后一种情况下请引用我的服务。这可能是因为它使用了 ExposeInvocationInterceptor.currentInvocation()
,并且执行另一个方法调用会弄乱它。
这是一个错误吗?基于代理的方法的一些限制?还是我必须简单地内联 MyService.aroundService
?
最佳答案
我重现了您的问题,并且还比较了普通 Java + AspectJ 中的类似设置(即没有 Spring 或 Spring AOP,仅使用方面切入点中使用的两个 Spring 注释)。那里不会出现问题。这是 Spring AOP 特有的东西,这是肯定的。
现在Spring使用AspectJ的切入点匹配结合自己的基于代理和委托(delegate)的AOP框架。在某个地方,这种边缘情况一定会弄乱 Spring 方面匹配的状态,从而导致您看到的行为。到目前为止我还没有调试过它,但从我现在看到的情况来看,我建议创建一个问题并查看维护人员对此有何评论。
这是我的 AspectJ MCVE证明问题不会在那里发生。顺便说一句,我不得不将包 aspect
重命名为 aop
因为在 AspectJ 中 aspect
是保留关键字。但是我也在Spring项目中重命名了它,以确保它与手头的问题无关,而且是无关的。
package aop.mcve;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
public void controllerMethod() {}
}
package aop.mcve;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public Object delegateTo(MyAspect.Callable<?> callable) throws Throwable {
return callable.call();
}
public void serviceMethod() {}
}
package aop.mcve;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
private final MyService myService = new MyService();
@Pointcut("within(aop.mcve..*) && !within(MyAspect) && execution(* *(..))")
public void inDomain() {}
@Pointcut("@target(org.springframework.stereotype.Service)")
public void inService() {}
@Pointcut("execution(* aop.mcve.MyService.*(..))")
public void inMyService() {}
@Pointcut("@target(org.springframework.web.bind.annotation.RestController)")
public void inController() {}
@Around("inDomain() && inController()")
public Object aroundController(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aroundController -> " + joinPoint);
return myService.delegateTo(joinPoint::proceed);
}
@Around("inDomain() && inService() && !inMyService()")
public Object aroundService(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aroundService -> " + joinPoint);
System.out.println("You should never see this message!");
return joinPoint.proceed();
}
public interface Callable<T> {
T call() throws Throwable;
}
}
package aop.mcve;
public class AspectMcveApplication {
public static void main(String[] args) throws Throwable {
new MyService().serviceMethod();
new MyController().controllerMethod();
}
}
控制台日志:
aroundController -> execution(void aop.mcve.MyController.controllerMethod())
如您所见,建议方法 aroundService(..)
不会像在 Spring AOP 中那样被触发。
更新:我修改了您的 MCVE 以使其可与 Spring AOP 和 AspectJ 一起运行,它在 Activity 时自动检测 AspectJ 的加载时编织器。我给你发了这个pull request .
关于java - Spring AOP - 切入点仅在从 lambda 调用 joinPoint.proceed 时应用于方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64718434/
我写了几个命令来转换数据框,但我想将我写的代码简化为四个部分。第 1,2 和 3 部分用于计算第 1、2 和 3 列(计算每列重复值的次数,并完成 0 和三列最大值之间的缺失数)。第四部分是加入前面的
我试图理解应用于函数的类型参数。 我想在下面的方法中使用通用类型,但为了我的理解使用 String 和 Int。 当我如下定义一个函数时 def myfunc[Int](f:String => I
我有一个像下面这样的 DIV: // link to some js .js 在 div 中呈现最新的文章摘要。然而,它在 Calibri
我在 GridView 中有以下列,一列是日期,另一列是美元金额。我应用了格式并将 HtmlEncode 属性设置为 false,但值仍然未格式化: 这就是这些值在 GridView 中的显示方式
假设我已经定义了这些类型: data Km = Km Float deriving (Show, Eq) data Mile = Mile Float deriving (Show, Eq
我有一个关于 value in context 的小问题。 取 Just 'a',所以在这种情况下 Maybe 类型上下文中的值是 'a' 采用[3],因此在这种情况下,[a] 类型上下文中的值为3
require(quantmod) require(PerformanceAnalytics) getSymbols('INTC') x<- monthlyReturn(INTC) rollapply
我正在使用 VBA 对“已应用字轨更改”文档进行更改。 红色段落结束标记是插入段落结束标记。(打开“跟踪更改”> 将光标放在第一段末尾 > 按 Enter > 插入新段落内容 > 格式风格不同) 我需
考虑以下代码: class A{ my_method(const B& b){ import_something_from_c(this, b.getC()); // does some
我正在为自定义 Material 分配图像。分配的图像看起来有点像素化,类似于此图像 我已经将抗锯齿设置为 4 倍。我该如何解决这个问题? 最佳答案 尝试将 Material 的 mipFilter
我将样式应用于 元素和 元素。是否可以在 上使用样式元素应用于 似乎不遵循 CSS 特异性的通常规则。这是真的吗? 示例:http://jsfiddle.net/59dpy/ 尝试将所有背景色设为红
有没有办法将垂直虚线边框应用于 没有他们(边界)合并?我说的是附图上的东西——有 3 个 这里的元素,每个元素包含 2 的。如果我申请 border-right: 1px dashed black到
当我在 CSS 中对主体应用线性渐变时,如下所示 body { background: linear-gradient(#10416b, black); } 它不会将它应用到整个网页,而是将它应用到页
当我将边框和边框半径应用于 td 时,内半径是一个直 Angular ,根本不是圆的。 最佳答案 问题很可能是背景不透明的子元素会剪掉边框的内半径。 要解决此问题,您可以在 td 上应用 overfl
基本上,我有一个小的 SVG,它使用一个组来定义一个可重用的符号。该组包括我想在 CSS 中设置动画的路径。我面临的问题是只有“原始”元素应用了 CSS,“使用过”的元素没有。 .player_arr
宽度属性在这里不起作用: td { height: 50px; width: 25px; border: 1px
我想要一个函数(例如)在两种情况下输出 Map 的所有值: Map map1 = new HashMap(); Map map2 = new HashMap(); output(map1, "1234
我被要求将我们应用中的警报对话框的外观与应用主题使用的外观相匹配。 我设法将样式应用于应用程序中的所有警报对话框,并将其用作应用程序主题的一部分,但有些情况下样式应用不正确。 例如,当警报对话框包含“
我有一个 CGPath(由 UIBezierPath 创建),我想通过应用 CGAffineTransformScale 将其缩放到我想要的任何大小。 这会影响我的绘图质量(在转换为图像时)吗?如果不
您好,我已经在 vector 上使用了一些 STL 算法,例如 find_if、count_if、sort、push_back 等。现在我想为所有容器对象( vector 、列表、映射、集合)制作一个
我是一名优秀的程序员,十分优秀!