【问题标题】:spring bean created by cglib throw no such method exceptioncglib创建的spring bean没有抛出这样的方法异常
【发布时间】:2016-05-20 15:05:42
【问题描述】:

我有几十节课,看起来像:

@Repository("jobProcessingDao")
@Transactional(readOnly = true)
public class JobProcessingDaoImpl extends BaseDaoImpl implements JobProcessingDao {
/*implementation interface methods*/   
}

只有这个会发生异常(并且只有这个是由 cglib 生成的)。

实际上,为什么 spring 使用 cglib 代理而不是 jdk?我的类实现了接口,我相信我没有为 ProxyFactoryBeanproxyInterfaces 设置任何值。

无论如何,我从 DefaultGeneratorStrategy#generate 类中得到字节格式,而这正是我实际得到的:

behemoth@Anechka ~ $ javap -c xf.class
Compiled from "<generated>"
public class package_name.dataaccesscomponent.impl.JobProcessingDaoImpl$$EnhancerByCGLIB$$f8ed98b3 extends package_name.dataaccesscomponent.impl.JobProcessingDaoImpl implements org.springframework.context.annotation.ConfigurationClassEnhancer$EnhancedConfiguration {
  static void CGLIB$STATICHOOK2();
    Code:
       0: new           #22                 // class java/lang/ThreadLocal
       3: dup
       4: invokespecial #25                 // Method java/lang/ThreadLocal."<init>":()V
       7: putstatic     #27                 // Field CGLIB$THREAD_CALLBACKS:Ljava/lang/ThreadLocal;
      10: iconst_0
      11: anewarray     #61                 // class java/lang/Object
      14: putstatic     #49                 // Field CGLIB$emptyArgs:[Ljava/lang/Object;
      17: ldc           #77                 // String package_name.impl.JobProcessingDaoImpl$$EnhancerByCGLIB$$f8ed98b3
      19: invokestatic  #83                 // Method java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
      22: astore_0
      23: iconst_2
      24: anewarray     #85                 // class java/lang/String
      27: dup
      28: iconst_0
      29: ldc           #86                 // String destroy
      31: aastore
      32: dup
      33: iconst_1
      34: ldc           #87                 // String ()V
      36: aastore
      37: ldc           #89                 // String org.springframework.beans.factory.DisposableBean
      39: invokestatic  #83                 // Method java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
      42: dup
      43: astore_1
      44: invokevirtual #93                 // Method java/lang/Class.getDeclaredMethods:()[Ljava/lang/reflect/Method;
      47: invokestatic  #99                 // Method net/sf/cglib/core/ReflectUtils.findMethods:([Ljava/lang/String;[Ljava/lang/reflect/Method;)[Ljava/lang/reflect/Method;
      50: dup
      51: iconst_0
      52: aaload
      53: putstatic     #47                 // Field CGLIB$destroy$10$Method:Ljava/lang/reflect/Method;
      56: aload_1
      57: aload_0
      58: ldc           #87                 // String ()V
      60: ldc           #86                 // String destroy
      62: ldc           #100                // String CGLIB$destroy$10
      64: invokestatic  #106                // Method net/sf/cglib/proxy/MethodProxy.create:(Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
      67: putstatic     #51                 // Field CGLIB$destroy$10$Proxy:Lnet/sf/cglib/proxy/MethodProxy;
      70: pop
      71: return
      72: return

  final void CGLIB$destroy$10() throws java.lang.Exception;
    Code:
       0: aload_0
       1: invokespecial #39                 // Method package_name/dataaccesscomponent/impl/JobProcessingDaoImpl.destroy:()V
       4: return

  public final void destroy() throws java.lang.Exception;
    Code:
       0: aload_0
       1: getfield      #41                 // Field CGLIB$CALLBACK_1:Lnet/sf/cglib/proxy/MethodInterceptor;
       4: dup
       5: ifnonnull     17
       8: pop
       9: aload_0
      10: invokestatic  #45                 // Method CGLIB$BIND_CALLBACKS:(Ljava/lang/Object;)V
      13: aload_0
      14: getfield      #41                 // Field CGLIB$CALLBACK_1:Lnet/sf/cglib/proxy/MethodInterceptor;
      17: dup
      18: ifnull        37
      21: aload_0
      22: getstatic     #47                 // Field CGLIB$destroy$10$Method:Ljava/lang/reflect/Method;
      25: getstatic     #49                 // Field CGLIB$emptyArgs:[Ljava/lang/Object;
      28: getstatic     #51                 // Field CGLIB$destroy$10$Proxy:Lnet/sf/cglib/proxy/MethodProxy;
      31: invokeinterface #57,  5           // InterfaceMethod net/sf/cglib/proxy/MethodInterceptor.intercept:(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;Lnet/sf/cglib/proxy/MethodProxy;)Ljava/lang/Object;
      36: return
      37: aload_0
      38: invokespecial #39                 // Method package_name/dataaccesscomponent/impl/JobProcessingDaoImpl.destroy:()V
      41: return

  public static net.sf.cglib.proxy.MethodProxy CGLIB$findMethodProxy(net.sf.cglib.core.Signature);
    Code:
       0: aload_0
       1: invokevirtual #65                 // Method java/lang/Object.toString:()Ljava/lang/String;
       4: dup
       5: invokevirtual #69                 // Method java/lang/Object.hashCode:()I
       8: tableswitch   { // 1460027707 to 1460027707
            1460027707: 28
               default: 40
          }
      28: ldc           #71                 // String destroy()V
      30: invokevirtual #75                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
      33: ifeq          41
      36: getstatic     #51                 // Field CGLIB$destroy$10$Proxy:Lnet/sf/cglib/proxy/MethodProxy;
      39: areturn
      40: pop
      41: aconst_null
      42: areturn

  public package_name.dataaccesscomponent.impl.JobProcessingDaoImpl$$EnhancerByCGLIB$$f8ed98b3();
    Code:
       0: aload_0
       1: dup
       2: invokespecial #107                // Method package_name/dataaccesscomponent/impl/JobProcessingDaoImpl."<init>":()V
       5: invokestatic  #45                 // Method CGLIB$BIND_CALLBACKS:(Ljava/lang/Object;)V
       8: return

  public static void CGLIB$SET_THREAD_CALLBACKS(net.sf.cglib.proxy.Callback[]);
    Code:
       0: getstatic     #27                 // Field CGLIB$THREAD_CALLBACKS:Ljava/lang/ThreadLocal;
       3: aload_0
       4: invokevirtual #112                // Method java/lang/ThreadLocal.set:(Ljava/lang/Object;)V
       7: return

  public static void CGLIB$SET_STATIC_CALLBACKS(net.sf.cglib.proxy.Callback[]);
    Code:
       0: aload_0
       1: putstatic     #115                // Field CGLIB$STATIC_CALLBACKS:[Lnet/sf/cglib/proxy/Callback;
       4: return

  static {};
    Code:
       0: invokestatic  #131                // Method CGLIB$STATICHOOK2:()V
       3: return
}

所以当我看到 cglibs 生成的类扩展了我的类时,这个类怎么可能没有这样的方法(在我的类中所有方法都是公共的)?

例外:

Caused by: java.lang.NoSuchMethodError: pakcage_name.dataaccesscomponent.JobProcessingDao.save(Lpakcage_name/businessentities/JobProcessing;)J
    at package_name.synchandler.impl.SyncHandlerImpl.createCompositeJobProcessing(SyncHandlerImpl.java:1232)

附: spring 3.1.0.RELEASE

【问题讨论】:

    标签: java spring cglib


    【解决方案1】:

    对于所提供的信息,我不能说我真的可以回答您的问题。但我可以说:

    • Jdk 代理比 CGLIB 代理快

    • 无法为类创建 Jdk 代理,只能为接口创建。

    • Spring 将始终支持 JDK 代理(因为它们显然更快 :))。但如果无法创建 JDK 代理,它将使用 CGLIB。

    所以,除非它不是 Spring 中的错误之类的,否则我认为您应该查看使用 JobProcessingDaoImpl 的类'别无选择,它必须使用CGLIB。

    我认为您还应该检查其他 DAO 实现的运行时(使用调试器或其他东西)——也许它们都用 JDK 代理包装。

    【讨论】:

    • 1.接口 2 的每次使用。JDK 代理包装的所有其他 dao 实现
    • 能否提供SyncHandlerImpl类源代码?另外-“保存”方法是否出现在 JobProcessingDao 中,因为它从 SyncHandlerImpl.createCompositeJobProcessing 中使用?
    • 组件公共类 SyncHandlerImpl 实现 SyncHandler { Autowired private JobProcessingDao jobProcessingDao;覆盖 public long createCompositeJobProcessing() { JobProcessing jobProcessing = new JobProcessing(); jobProcessing.setCreateTime(System.currentTimeMillis()); jobProcessing.setComposite(true);返回 jobProcessingDao.save(jobProcessing); } }
    • 这很有趣,因为它只是偶尔发生,而且只发生在我身上(在大约 30 多名同事的团队中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2014-10-28
    • 2015-05-18
    • 2020-03-24
    • 2023-04-04
    相关资源
    最近更新 更多