java 如何拦截Spring中的静态方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4993947/
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
How to intercept static methods in Spring?
提问by AHungerArtist
Subject line basically says it all. I have a static method I want to intercept so that around advice can be applied to it. I can get this to work with any non-static methods but I'm unsure how to allow static methods to be intercepted.
主题行基本上说明了一切。我有一个我想拦截的静态方法,以便可以将周围的建议应用于它。我可以让它与任何非静态方法一起使用,但我不确定如何允许拦截静态方法。
回答by limc
You can't do that with Spring AOP, because it is proxy based. You have to use AspectJ. Take a look at this simple example: http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/
你不能用 Spring AOP 做到这一点,因为它是基于代理的。您必须使用 AspectJ。看看这个简单的例子:http: //blog.jayway.com/2007/02/16/static-mock-using-aspectj/
回答by Filip
See PowerMock https://github.com/powermock/powermockit can mock static methods
参见 PowerMock https://github.com/powermock/powermock它可以模拟静态方法
Taken from the blog post comment from LIMC's answer
摘自 LIMC 回答的博客文章评论