{"record":{"id":"76caa402b4ef38b6","repo":"spring-projects/spring-framework","slug":"target-needs-to-be-a-bean-reference-not-a-bean","errorCode":null,"errorMessage":"'target' needs to be a bean reference, not a bean name as value","messagePattern":"'target' needs to be a bean reference, not a bean name as value","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java","lineNumber":141,"sourceCode":"\tpublic void setProxyClassLoader(ClassLoader classLoader) {\n\t\tthis.proxyClassLoader = classLoader;\n\t}\n\n\t@Override\n\tpublic void setBeanClassLoader(ClassLoader classLoader) {\n\t\tif (this.proxyClassLoader == null) {\n\t\t\tthis.proxyClassLoader = classLoader;\n\t\t}\n\t}\n\n\n\t@Override\n\tpublic void afterPropertiesSet() {\n\t\tif (this.target == null) {\n\t\t\tthrow new IllegalArgumentException(\"Property 'target' is required\");\n\t\t}\n\t\tif (this.target instanceof String) {\n\t\t\tthrow new IllegalArgumentException(\"'target' needs to be a bean reference, not a bean name as value\");\n\t\t}\n\t\tif (this.proxyClassLoader == null) {\n\t\t\tthis.proxyClassLoader = ClassUtils.getDefaultClassLoader();\n\t\t}\n\n\t\tProxyFactory proxyFactory = new ProxyFactory();\n\n\t\tif (this.preInterceptors != null) {\n\t\t\tfor (Object interceptor : this.preInterceptors) {\n\t\t\t\tproxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));\n\t\t\t}\n\t\t}\n\n\t\t// Add the main interceptor (typically an Advisor).\n\t\tproxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));\n\n\t\tif (this.postInterceptors != null) {\n\t\t\tfor (Object interceptor : this.postInterceptors) {","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java#L123-L159","documentation":"AbstractSingletonProxyFactoryBean.afterPropertiesSet() checks (line 140) whether target is a String and rejects it: the target must be the actual bean instance (a reference), not the bean's name. This catches the classic XML mistake of using value= (a literal string) instead of ref= (a bean reference).","triggerScenarios":"Setting target to a String value - e.g. XML <property name=\"target\" value=\"myService\"/> (value, not ref) - or programmatically calling setTarget(\"myService\") passing the bean name instead of the bean object.","commonSituations":"XML config using value=\"beanName\" where ref=\"beanName\" was intended; programmatic wiring that passes a bean name string by mistake; copy-paste from a property that genuinely takes a string.","solutions":["In XML, change <property name=\"target\" value=\"myService\"/> to <property name=\"target\" ref=\"myService\"/>","Programmatically, fetch the bean instance first (context.getBean(\"myService\")) and pass that to setTarget(...), never the name string","Review all target= attributes on proxy factory beans to confirm they use ref semantics"],"exampleFix":"// before\n<property name=\"target\" value=\"myService\"/>\n\n// after\n<property name=\"target\" ref=\"myService\"/>","handlingStrategy":"type-guard","validationCode":"Object t = factoryBean.getTarget();\nif (t instanceof String) {\n  throw new IllegalStateException(\"target must be a bean reference, not the name: \" + t);\n}","typeGuard":"private static boolean targetIsReference(AbstractSingletonProxyFactoryBean fb) {\n  Object t = (fb == null) ? null : fb.getTarget();\n  return t != null && !(t instanceof String);\n}","tryCatchPattern":null,"preventionTips":["In XML always use ref= for the target property, never value=","In code, pass the bean instance from context.getBean(...) rather than the name"],"tags":["spring-aop","config","proxy-factory","bean-reference"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}