{"record":{"id":"8edbd9448854edfd","repo":"mybatis/mybatis-3","slug":"cannot-enable-lazy-loading-because-cglib-is-not-av","errorCode":null,"errorMessage":"Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.","messagePattern":"Cannot enable lazy loading because CGLIB is not available\\. Add CGLIB to your classpath\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/loader/cglib/CglibProxyFactory.java","lineNumber":59,"sourceCode":"import org.apache.ibatis.reflection.property.PropertyNamer;\nimport org.apache.ibatis.session.Configuration;\n\n/**\n * @author Clinton Begin\n *\n * @deprecated Since 3.5.10, use Javassist instead.\n */\n@Deprecated\npublic class CglibProxyFactory implements ProxyFactory {\n\n  private static final String FINALIZE_METHOD = \"finalize\";\n  private static final String WRITE_REPLACE_METHOD = \"writeReplace\";\n\n  public CglibProxyFactory() {\n    try {\n      Resources.classForName(\"net.sf.cglib.proxy.Enhancer\");\n    } catch (Throwable e) {\n      throw new IllegalStateException(\n          \"Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.\", e);\n    }\n  }\n\n  @Override\n  public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration,\n      ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {\n    return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory,\n        constructorArgTypes, constructorArgs);\n  }\n\n  public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties,\n      ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {\n    return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes,\n        constructorArgs);\n  }\n\n  static Object createStaticProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes,","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/loader/cglib/CglibProxyFactory.java#L41-L77","documentation":"CglibProxyFactory's constructor probes for net.sf.cglib.proxy.Enhancer on the classpath; if absent it throws IllegalStateException telling you CGLIB is missing. This proxy factory is deprecated since 3.5.10 in favor of the Javassist one, and cglib is an optional dependency that must be added separately.","triggerScenarios":"new CglibProxyFactory() (explicitly, or via configuration.setProxyFactory(new CglibProxyFactory())) when net.sf.cglib is not on the runtime classpath; Resources.classForName throws and the constructor wraps it in IllegalStateException.","commonSituations":"Upgrading MyBatis past 3.5.10 where CGLIB is no longer the default; migrating to a JDK/JDK17+ environment where cglib's old versions break; explicitly choosing cglib in old tutorials without adding the cglib dependency.","solutions":["Prefer the default JavassistProxyFactory (remove any explicit CglibProxyFactory configuration) — CglibProxyFactory is deprecated","If CGLIB is required, add the dependency: org.mybatis:mybatis-cglib or net.sf.cglib:cglib (nodep variant if needed)","Ensure the chosen proxy factory matches what is actually on the runtime classpath in every deployment module"],"exampleFix":"// before\nconfiguration.setProxyFactory(new org.apache.ibatis.executor.loader.cglib.CglibProxyFactory());\n// after: default since 3.5.x, cglib deprecated\nconfiguration.setProxyFactory(new org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory());\n// or simply keep the default proxyFactory","handlingStrategy":"validation","validationCode":"// before opting into cglib, probe availability the same way MyBatis does\ntry { Class.forName(\"net.sf.cglib.proxy.Enhancer\"); } catch (ClassNotFoundException e) { throw new IllegalStateException(\"CGLIB missing - keep default proxy factory or add dependency\", e); }","typeGuard":"static boolean cglibAvailable() { try { Class.forName(\"net.sf.cglib.proxy.Enhancer\"); return true; } catch (ClassNotFoundException e) { return false; } }","tryCatchPattern":"try { new CglibProxyFactory(); } catch (IllegalStateException e) { // fall back to default javassist factory configuration.setProxyFactory(new JavassistProxyFactory()); }","preventionTips":["Do not configure CglibProxyFactory on MyBatis >= 3.5.10 — it is deprecated","Verify optional proxy jars exist in every deployment artifact","Pin proxy factory choice in one shared config module"],"tags":["mybatis","lazy-loading","cglib","classpath","dependency"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}