java - Bean creation failed while doing autowiring -
i doing simple test on autowiring in spring bean autowiring not run if interface passed constructor argument. here code bean
package profile; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; @component public class user { private createuser user; @autowired public user(createuser user) { this.user = user; } public void adduser(string name, string contact) { system.out.println("adding "+name+" , "+contact+"....."); } public void showuser() { system.out.println("added successfully"); } }
my automatic config class
package profile; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; @configuration @componentscan public class userconfig { }
and main class
package profile; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class usermain { public static void main(string ar[]) { annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(userconfig.class); user users = context.getbean(user.class); users.adduser("test", "122466"); users.showuser(); context.close(); } }
i getting following error:
run: sep 12, 2017 12:20:28 pm org.springframework.context.annotation.annotationconfigapplicationcontext preparerefresh info: refreshing org.springframework.context.annotation.annotationconfigapplicationcontext@1ed6993a: startup date [tue sep 12 12:20:28 ist 2017]; root of context hierarchy sep 12, 2017 12:20:28 pm org.springframework.context.annotation.annotationconfigapplicationcontext refresh warning: exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'user' defined in file [d:\jsonwithjava\build\classes\profile\user.class]: unsatisfied dependency expressed through constructor parameter 0; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'profile.createuser' available: expected @ least 1 bean qualifies autowire candidate. dependency annotations: {} exception in thread "main" org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'user' defined in file [d:\jsonwithjava\build\classes\profile\user.class]: unsatisfied dependency expressed through constructor parameter 0; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'profile.createuser' available: expected @ least 1 bean qualifies autowire candidate. dependency annotations: {} @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:749) @ org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:189) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1193) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1095) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:761) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:867) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:543) @ org.springframework.context.annotation.annotationconfigapplicationcontext.<init>(annotationconfigapplicationcontext.java:84) @ profile.usermain.main(usermain.java:9) caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'profile.createuser' available: expected @ least 1 bean qualifies autowire candidate. dependency annotations: {} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenomatchingbeanfound(defaultlistablebeanfactory.java:1493) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1104) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066) @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:835) @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ... 14 more c:\users\admin\appdata\local\netbeans\cache\8.2\executor-snippets\run.xml:53: java returned: 1 build failed (total time: 1 second)
but if use default constructor program works fine this:
package profile; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; @component public class user { private createuser user; @autowired public user() { } public void adduser(string name, string contact) { system.out.println("adding "+name+" , "+contact+"....."); } public void showuser() { system.out.println("added successfully"); } }
the code runs perfectly. here output:
run: sep 12, 2017 12:21:49 pm org.springframework.context.annotation.annotationconfigapplicationcontext preparerefresh info: refreshing org.springframework.context.annotation.annotationconfigapplicationcontext@1ed6993a: startup date [tue sep 12 12:21:49 ist 2017]; root of context hierarchy adding test , 122466..... added sep 12, 2017 12:21:49 pm org.springframework.context.annotation.annotationconfigapplicationcontext doclose info: closing org.springframework.context.annotation.annotationconfigapplicationcontext@1ed6993a: startup date [tue sep 12 12:21:49 ist 2017]; root of context hierarchy build successful (total time: 0 seconds)
please help....
in configuration class write below code:
@configuration @componentscan public class userconfig { //if default constructor in createuser @bean public createuser createuserbeancreate(){ return new createuser(); } }
another option use @bean above createuser class:
@bean public class createuser{ }
if creatweuser interface (which case), need implementation of createuser , use @bean annotation above class, spirng ioc container can autowire createuser implementation whenever use autowire above createuser. hope helps.
Comments
Post a Comment