简体   繁体   English

何时以及如何创建 cglib 代理的组件实例

[英]When and how cglib-proxied component instance is created

I'd like to learn if there are some rules / conditions that a Spring component is wrapped (proxied) by CGLIB.我想了解是否有一些规则/条件 Spring 组件由 CGLIB 包装(代理)。 For example, take this case:例如,以这种情况为例:

@Component
public class TestComponent {
}

@Service
//@Transactional(rollbackFor = Throwable.class)
public class ProcessComponent {

    @Autowired
    private TestComponent testComponent;

    public void doSomething(int key) {
      // try to debug "testComponent" instance here ...
    }

}

If we let it like this and debug the testComponent field inside the method, then we'll see that it's not wrapped by CGLIB.如果我们让它这样并调试方法内的testComponent字段,那么我们将看到它没有被 CGLIB 包装。

Now if we uncomment the @Transactional annotation and debug, we'll find that the instance is wrapped: it's of type ProcessComponent$$EnhancerByCGLIB$$14456 or something like that.现在,如果我们取消注释@Transactional注释并进行调试,我们会发现该实例已被包装:它的类型为ProcessComponent$$EnhancerByCGLIB$$14456或类似的东西。 It's clearly because Spring needs to create a proxy class to handle the transaction support.这显然是因为 Spring 需要创建一个代理 class 来处理事务支持。

But I'm wondering, is there any way that we can detect how and when does this wrapping happen?但我想知道,有什么方法可以检测到这种包装是如何以及何时发生的? For example, some specific locations in Spring's source code to debug into to find more information;例如,在 Spring 的源代码中的一些特定位置进行调试以查找更多信息; or some documentations on the rules of how they decide to create a proxy.或有关他们如何决定创建代理的规则的一些文档。

For your information, I need to know about this because I'm facing a situation where some component ( not @Transactional, above example is just for demonstrating purpose ) in my application suddenly becomes proxied (I found a revision a bit in the past where it is not).为了您的信息,我需要知道这一点,因为我面临这样一种情况,即我的应用程序中的某些组件(不是@Transactional,上面的示例仅用于演示目的突然被代理(我在过去发现了一个修订版它不是)。 The most important issue is that this'll affect such components that also contain public final methods and another issue (also of importance) is that there must have been some unexpected changes in the design / structure of classes.最重要的问题是这会影响那些也包含public final方法的组件,另一个(同样重要的)问题是类的设计/结构必须有一些意想不到的变化。 For these kind of issues, of course we must try to find out what happened / who did the change that led to this etc...对于这类问题,我们当然必须尝试找出发生了什么/谁做了导致这种情况的改变等等......

One note is that we have just upgraded our application from Spring Boot 2.1.0RELEASE to 2.1.10RELEASE.需要注意的是,我们刚刚将应用程序从 Spring Boot 2.1.0RELEASE 升级到 2.1.10RELEASE。 And checking the code revision by revision up till now is not feasible, because there have been quite a lot of commits.到目前为止,逐个修订检查代码修订是不可行的,因为已经有相当多的提交。

Any kind of help would be appreciated, thanks in advance.任何形式的帮助将不胜感激,在此先感谢。

You could debug into org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(Class, String, TargetSource).您可以调试到 org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(Class, String, TargetSource)。 If any advisor is found, the bean will be proxied.如果找到任何顾问,该 bean 将被代理。

If you use a @Lookup method injection it will also proxy the component class.如果您使用@Lookup 方法注入,它还将代理组件 class。

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

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