mybatis/mybatis-3 · error · IllegalStateException

Cannot enable lazy loading because CGLIB is not available. A

Error message

Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.

What it means

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.

Source

Thrown at src/main/java/org/apache/ibatis/executor/loader/cglib/CglibProxyFactory.java:59

import org.apache.ibatis.reflection.property.PropertyNamer;
import org.apache.ibatis.session.Configuration;

/**
 * @author Clinton Begin
 *
 * @deprecated Since 3.5.10, use Javassist instead.
 */
@Deprecated
public class CglibProxyFactory implements ProxyFactory {

  private static final String FINALIZE_METHOD = "finalize";
  private static final String WRITE_REPLACE_METHOD = "writeReplace";

  public CglibProxyFactory() {
    try {
      Resources.classForName("net.sf.cglib.proxy.Enhancer");
    } catch (Throwable e) {
      throw new IllegalStateException(
          "Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
    }
  }

  @Override
  public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration,
      ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory,
        constructorArgTypes, constructorArgs);
  }

  public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties,
      ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes,
        constructorArgs);
  }

  static Object createStaticProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes,

View on GitHub (pinned to 008069adb1)

Solutions

  1. Prefer the default JavassistProxyFactory (remove any explicit CglibProxyFactory configuration) — CglibProxyFactory is deprecated
  2. If CGLIB is required, add the dependency: org.mybatis:mybatis-cglib or net.sf.cglib:cglib (nodep variant if needed)
  3. Ensure the chosen proxy factory matches what is actually on the runtime classpath in every deployment module

Example fix

// before
configuration.setProxyFactory(new org.apache.ibatis.executor.loader.cglib.CglibProxyFactory());
// after: default since 3.5.x, cglib deprecated
configuration.setProxyFactory(new org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory());
// or simply keep the default proxyFactory
Defensive patterns

Strategy: validation

Validate before calling

// before opting into cglib, probe availability the same way MyBatis does
try { Class.forName("net.sf.cglib.proxy.Enhancer"); } catch (ClassNotFoundException e) { throw new IllegalStateException("CGLIB missing - keep default proxy factory or add dependency", e); }

Type guard

static boolean cglibAvailable() { try { Class.forName("net.sf.cglib.proxy.Enhancer"); return true; } catch (ClassNotFoundException e) { return false; } }

Try / catch

try { new CglibProxyFactory(); } catch (IllegalStateException e) { // fall back to default javassist factory configuration.setProxyFactory(new JavassistProxyFactory()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14). Data as JSON: /api/errors/8edbd9448854edfd. Report an issue: GitHub.