简体   繁体   English

Spring 代理:BeanPostProcessor 还是 AOP?

[英]Spring proxy: BeanPostProcessor or AOP?

We can create proxy for the target bean in the postProcessAfterInitialization() method of BeanPostProcessor.我们可以在 BeanPostProcessor 的 postProcessAfterInitialization() 方法中为目标 bean 创建代理。

Or we can use AOP - and proxy will also be created through Dynamic Proxy or CGlib.或者我们可以使用 AOP - 代理也将通过动态代理或 CGlib 创建。

What is the difference?有什么区别?

When should we use BPP and when AOP?什么时候用BPP,什么时候用AOP?

As far as I know bean-post-processor is a more powerful tool.据我所知,bean-post-processor 是一个更强大的工具。 It can complete replace the bean (so you can even shoot in your own foot).它可以完全取代豆子(所以你甚至可以用自己的脚射击)。 On the other hand AOP approach has very handy pattern matching solution.另一方面,AOP 方法有非常方便的模式匹配解决方案。 In a bean-post-processor approach you get an Object and you have to recognize what is its type.在 bean 后处理器方法中,您会得到一个Object ,您必须识别它的类型。 Moreover AOP approach is more about adding a behaviour related to pattern-selected methods' execution.此外,AOP 方法更多的是添加与模式选择方法的执行相关的行为。 With bean-post-processor you can replace (or just modify) a whole bean which possibly affects all of its method.使用 bean 后处理器,您可以替换(或修改)可能影响其所有方法的整个 bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM