spring - @Autowired(required=false) on constructor giving NoSuchBeanDefinitionException -


in spring application, have used @autowired(required=false) on constructor. means if bean autowired not available in xml file, no nosuchbeandefinitionexception should thrown (required=false) mentioned. getting unsatisfieddependencyexception, nosuchbeandefinitionexception exception.

---- texteditor

public class texteditor {      private spellchecker x;     private string name;          @autowired(required=false)     public texteditor(spellchecker x) {         system.out.println("inside texteditor constructor." );         this.x = x;     }       public spellchecker gety() {         return x;     }      public void setname(string name) {         this.name = name;     }      public string getname() {         return name;     }      public void spellcheck() {         x.checkspelling();     } } 

---- spellchecker

public class spellchecker {     public spellchecker() {         system.out.println("inside spellchecker constructor.");     }      public void checkspelling() {         system.out.println("inside checkspelling.");     } } 

---- beans.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:aks="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"     aks:schemalocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd">      <context:annotation-config />      <bean id="texteditor" class="com.tutorialspoint.texteditor">         <!-- <property name="x" ref="a" /> -->         <property name="name" value="generic text editor" />     </bean>      <!-- <bean id="a" class="com.tutorialspoint.spellchecker" /> -->  </beans>  ---- mainapp public class mainapp {     public static void main(string[] args) {         applicationcontext context = new classpathxmlapplicationcontext(                 "beans.xml");//beans.xml, beans1.xml         texteditor te = (texteditor) context.getbean("texteditor");         //te.spellcheck();         system.out.println(te.gety());     } } 

--- console(actual result)

apr 24, 2014 4:30:00 pm org.springframework.context.support.abstractapplicationcontext preparerefresh info: refreshing org.springframework.context.support.classpathxmlapplicationcontext@15eb0a9: startup date [thu apr 24 16:30:00 ist 2014]; root of context hierarchy apr 24, 2014 4:30:00 pm org.springframework.beans.factory.xml.xmlbeandefinitionreader loadbeandefinitions info: loading xml bean definitions class path resource [beans.xml] apr 24, 2014 4:30:00 pm org.springframework.beans.factory.support.defaultlistablebeanfactory preinstantiatesingletons info: pre-instantiating singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@2d9c06: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,texteditor]; root of factory hierarchy apr 24, 2014 4:30:00 pm org.springframework.beans.factory.support.defaultsingletonbeanregistry destroysingletons info: destroying singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@2d9c06: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,texteditor]; root of factory hierarchy exception in thread "main" org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'texteditor' defined in class path resource [beans.xml]: unsatisfied dependency expressed through constructor argument index 0 of type [com.tutorialspoint.spellchecker]: : no matching bean of type [com.tutorialspoint.spellchecker] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.tutorialspoint.spellchecker] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}     @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:730)     @ org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:196)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1002)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:906)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:484)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:455)     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:293)     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222)     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:290)     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:192)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:585)     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:895)     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:425)     @ org.springframework.context.support.classpathxmlapplicationcontext.<init>(classpathxmlapplicationcontext.java:139)     @ org.springframework.context.support.classpathxmlapplicationcontext.<init>(classpathxmlapplicationcontext.java:83)     @ com.tutorialspoint.mainapp.main(mainapp.java:8) caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.tutorialspoint.spellchecker] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:924)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:793)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:707)     @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:795)     @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:723)     ... 15 more 

--- expected result program should run without exception @autowire(required=false) mentioned constructor. if bean not found exception should not come since (required=false) mentioned.

this exception happens because required = false not mean inject null. when applied constructors spring try decide constructor best suited creating instance. in case have 1 constructor need spellchecker no object of type.

as referenced in spring doc (http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/beans/factory/annotation/autowired.html):

only 1 constructor (at max) of given bean class may carry annotation, indicating constructor autowire when used spring bean. such constructor not have public.

anyway can add default constructor (it can private) when spring can't @autowired magic use one. in texteditor class add:

@deprecated private texteditor() {     // leave x = null or create default value field      // if have 1 (eg. x = new defaultspellchecker();) } 

note @deprecated used avoid compiler warn you have private constructor no 1 uses.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -