{"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":"exception","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java#L123-L159","documentation":"Thrown by AbstractSingletonProxyFactoryBean.afterPropertiesSet when setTarget was given a String value. Spring requires the actual target object (or a TargetSource), not the bean's name; this guard catches the classic XML mistake of using value=\"beanName\" (which injects a literal String) instead of ref=\"beanName\" (which injects the bean instance). It prevents silent, confusing failures downstream where the proxy would try to wrap a java.lang.String.","triggerScenarios":"In XML, writing <property name=\"target\" value=\"myService\"/> instead of ref=\"myService\"/>. Programmatically, calling factoryBean.setTarget(\"myServiceName\") passing a String instead of the bean instance. Any path that assigns a String literal where a target object is required.","commonSituations":"Editors/IDEs auto-completing value= instead of ref=; converting annotations to XML and forgetting the ref semantics; beginners confusing bean name with bean reference.","solutions":["Change value=\"beanName\" to ref=\"beanName\" in the target property declaration.","If configuring in Java, inject the actual bean instance: factoryBean.setTarget(context.getBean(\"myService\")) or better, the autowired instance directly.","Run a quick grep over the config for name=\"target\" value= as a sanity check."],"exampleFix":"// before\n<property name=\"target\" value=\"myService\"/>\n\n// after\n<property name=\"target\" ref=\"myService\"/>","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Guard at the boundary where you wire targets programmatically:\nstatic void ensureTargetIsReference(Object target) {\n  if (target == null) throw new IllegalArgumentException(\"target required\");\n  if (target instanceof String s) {\n    throw new IllegalArgumentException(\n      \"'target' must be a bean reference (the instance), not the bean name '\" + s + \"'\");\n  }\n}","tryCatchPattern":null,"preventionTips":["In XML, always use ref= for the target property, never value=.","When converting annotations to XML, remember Spring injects instances via ref.","Grep the config for <property name=\\\"target\\\" value= as a static check."],"tags":["spring-aop","configuration","xml","bean-reference"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}