简体   繁体   中英

spring bean created by cglib throw no such method exception

I have a few tens of class which looks like:

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

exception happens only for this one(and only this one has been generated by cglib).

actually, why spring use cglib proxy instead of jdk? my class implements interface, and I believe I'm not set any value to proxyInterfaces of ProxyFactoryBean .

Anyway, I get from DefaultGeneratorStrategy#generate class in byte format and this what I actually get:

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
}

So as I see cglibs generated class extends my class, and how it even be possible that this class haven't such method(in my class all methods is public)?

Exception:

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)

PS spring 3.1.0.RELEASE

I can't say that I can really answer your question our of the provided information. But I can say the following:

  • Jdk proxies are faster than CGLIB proxies

  • Jdk proxies can't be created for classes, only for interfaces.

  • Spring will always favour JDK proxies (because they're faster obviously :)). But if it's impossible to create a JDK proxy it will use CGLIB.

So, unless its not a bug in Spring or something, I think you should take a look on classes that use JobProcessingDaoImpl The chances are that the relevant data field is not wired by interface, but by class, so Spring doesn't have a choice and it has to use CGLIB.

I think you should also check a runtime (with debugger or something) other DAO implementations - maybe all of them are wrapped with JDK Proxies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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