{"id":"3f3ce769e7b541ed","repo":"spring-projects/spring-framework","slug":"unable-to-instantiate-proxy-using-objenesis-and-r","errorCode":null,"errorMessage":"Unable to instantiate proxy using Objenesis, and regular proxy instantiation via default constructor fails as well","messagePattern":"Unable to instantiate proxy using Objenesis, and regular proxy instantiation via default constructor fails as well","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java","lineNumber":86,"sourceCode":"\t\t\t}\n\t\t\tcatch (Throwable ex) {\n\t\t\t\tlogger.debug(\"Unable to instantiate proxy using Objenesis, \" +\n\t\t\t\t\t\t\"falling back to regular proxy construction\", ex);\n\t\t\t}\n\t\t}\n\n\t\tif (proxyInstance == null) {\n\t\t\t// Regular instantiation via default constructor...\n\t\t\ttry {\n\t\t\t\tConstructor<?> ctor = (this.constructorArgs != null ?\n\t\t\t\t\t\tproxyClass.getDeclaredConstructor(this.constructorArgTypes) :\n\t\t\t\t\t\tproxyClass.getDeclaredConstructor());\n\t\t\t\tReflectionUtils.makeAccessible(ctor);\n\t\t\t\tproxyInstance = (this.constructorArgs != null ?\n\t\t\t\t\t\tctor.newInstance(this.constructorArgs) : ctor.newInstance());\n\t\t\t}\n\t\t\tcatch (Throwable ex) {\n\t\t\t\tthrow new AopConfigException(\"Unable to instantiate proxy using Objenesis, \" +\n\t\t\t\t\t\t\"and regular proxy instantiation via default constructor fails as well\", ex);\n\t\t\t}\n\t\t}\n\n\t\t((Factory) proxyInstance).setCallbacks(callbacks);\n\t\treturn proxyInstance;\n\t}\n\n}\n","sourceCodeStart":68,"sourceCodeEnd":96,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java#L68-L96","documentation":"Thrown by ObjenesisCglibAopProxy when the CGLIB proxy class was generated but no instance could be created. Spring first tries Objenesis (a byte-code library that bypasses constructors), and if that fails it falls back to invoking a default (or configured) constructor via reflection. This exception means BOTH paths failed, so the proxied class effectively cannot be instantiated. It wraps the underlying cause (typically a constructor exception or Objenesis limitation) as its cause.","triggerScenarios":"Triggered during getProxy() for a class-based (proxyTargetClass=true) AOP proxy when the target class has no accessible no-arg constructor AND Objenesis cannot create the instance (e.g. objenesis.isWorthTrying() returned false or threw). Also when a configured constructor-arg type does not match any declared constructor of the generated proxy class.","commonSituations":"Proxied class declares only a non-default constructor with required args and no matching constructor-args were supplied to ProxyFactory; running on a JVM/module configuration where Objenesis is disabled; the target's default constructor itself throws; proxying a class with a private no-arg constructor on a strict JPMS setup that blocks deep reflection.","solutions":["Ensure the target class has a public no-arg constructor, or supply matching constructor-arg types/values to ProxyFactory.","If using Spring DI, ensure the proxied bean is itself constructed by Spring and the proxy wraps an instance rather than needing instantiation.","Upgrade/verify the Objenesis dependency is on the classpath and not disabled (SpringObjenesis.isWorthTrying()).","Inspect the wrapped cause in the stack trace - it usually names the exact missing constructor or the constructor failure."],"exampleFix":"// before\nclass Service { Service(Dep dep) { ... } } // only one constructor\nProxyFactory pf = new ProxyFactory();\npf.setTarget(new Service(dep));\n\n// after: provide constructor args to the proxy\nProxyFactory pf = new ProxyFactory();\npf.setTargetClass(Service.class);\npf.setConstructorArgTypes(new Class[]{Dep.class});\npf.setConstructorArguments(new Object[]{dep});","handlingStrategy":"validation","validationCode":"// Before building a class proxy, ensure instantiability\nClass<?> targetClass = pf.getTargetClass();\nboolean hasNoArgCtor = java.util.Arrays.stream(targetClass.getDeclaredConstructors())\n    .anyMatch(c -> c.getParameterCount() == 0);\nif (!hasNoArgCtor && (pf.getConstructorArguments() == null)) {\n    throw new IllegalStateException(\"Target has no usable constructor for CGLIB proxy: \" + targetClass);\n}","typeGuard":"public static boolean canProxyInstantiate(ProxyFactory pf) {\n    Class<?> c = pf.getTargetClass();\n    return c != null && !Modifier.isAbstract(c.getModifiers()) && !Modifier.isInterface(c.getModifiers());\n}","tryCatchPattern":"try {\n    return pf.getProxy();\n} catch (AopConfigException ex) {\n    Throwable cause = ex.getCause();\n    if (cause != null && cause.getMessage().contains(\"constructor\")) {\n        throw new IllegalStateException(\"Missing no-arg constructor for proxy target\", cause);\n    }\n    throw ex;\n}","preventionTips":["Give proxy target classes a non-private no-arg constructor.","Prefer setting a target instance on ProxyFactory rather than proxying just the class.","Keep Objenesis on the classpath and unrestrict deep reflection where proxies are needed."],"tags":["aop","cglib","proxy","instantiation","objenesis"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}