java - how to solve "injection of autowired dependencies failed" issue? -
i try develop server program. so, test program using junit4. injection of autowired dependencies failed error occurs. don't know how solve problem. please me.
this context-servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="kr.ac.jbnu.sql.soremore" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.urlbasedviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" /> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="rdmldbmgmt" class="kr.ac.jbnu.sql.soremore.service.rdml.rdmldbmgmtimpl" /> </beans>
this interface.
package kr.ac.jbnu.sql.soremore.service; import java.util.arraylist; import javax.jcr.node; import javax.jcr.repositoryexception; import kr.ac.jbnu.sql.soremore.model.rdml; public interface irdmldbmgmt { public boolean storerdml(rdml rdml) throws rdmldbexception, revisioncontrolexception, repositoryexception, exception ; public rdml retrieverecentrdml(string id) throws revisioncontrolexception, exception; public arraylist<string> retrieverdmlhistory(string id) throws revisioncontrolexception, exception; public rdml retrieverdml(string keyword) throws rdmldbexception, repositoryexception, exception; }
this implementation code of interface.
package kr.ac.jbnu.sql.soremore.service.rdml; import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.util.arraylist; import java.util.iterator; import java.util.linkedlist; import java.util.list; import javax.jcr.node; import javax.jcr.nodeiterator; import javax.jcr.pathnotfoundexception; import javax.jcr.repository; import javax.jcr.repositoryexception; import javax.jcr.session; import javax.jcr.simplecredentials; import javax.jcr.unsupportedrepositoryoperationexception; import javax.jcr.valueformatexception; import javax.jcr.query.query; import javax.jcr.query.querymanager; import javax.jcr.query.queryresult; import javax.jcr.version.version; import javax.jcr.version.versionhistory; import javax.jcr.version.versioniterator; import javax.jcr.version.versionmanager; import javax.xml.parsers.parserconfigurationexception; import kr.ac.jbnu.sql.soremore.controller.soremorecontroller; import kr.ac.jbnu.sql.soremore.model.rdml; import kr.ac.jbnu.sql.soremore.service.irdmldbmgmt; import kr.ac.jbnu.sql.soremore.service.rdmldbexception; import kr.ac.jbnu.sql.soremore.service.revisioncontrolexception; import org.apache.jackrabbit.commons.jcrutils; import org.apache.log4j.logger; import org.jdom2.attribute; import org.jdom2.document; import org.jdom2.element; import org.jdom2.output.xmloutputter; import org.springframework.stereotype.service; @service public class rdmldbmgmtimpl implements irdmldbmgmt { static logger logger = logger.getlogger(soremorecontroller.class.getname()); static private int docnumber = 0; private repository repository; private session session; private node root; private node basewp = null; private node inputedwp = null; private list<string> remainedbaseworkproductitemids; private list<string> remainedinputedworkproductitemids; private versionmanager vm; private querymanager querymanager; public rdmldbmgmtimpl() throws parserconfigurationexception, unsupportedrepositoryoperationexception, repositoryexception { super(); try { repository = jcrutils.getrepository(); session = repository.login(new simplecredentials("admin", "admin" .tochararray())); root = session.getrootnode(); vm = session.getworkspace().getversionmanager(); querymanager = session.getworkspace().getquerymanager(); } catch (repositoryexception e) { e.printstacktrace(); } remainedbaseworkproductitemids = new linkedlist<string>(); remainedinputedworkproductitemids = new linkedlist<string>(); } @override public boolean storerdml(rdml rdml) throws rdmldbexception, revisioncontrolexception, repositoryexception, exception { document rdmlasdocument = rdml.getrdmlasdocument(); string id = rdml.getidfromdocument(rdmlasdocument); rdml currdml = retrieverecentrdml(id); if (currdml != null && currdml.getrdmlasnode() != null) basewp = currdml.getrdmlasnode(); else basewp = null; inputedwp = convertdoctonode(rdmlasdocument, id); // version 정보 갱신 if (basewp == null) { initialversioning(inputedwp); } else { versionup(); } return true; } // 수정필요 node -> rdml 어떻게? // node이름 가져올 수 있나???????? @override public rdml retrieverecentrdml(string id) throws revisioncontrolexception, exception { nodeiterator workproducts = root.getnodes("workproduct"); node workproduct = null; rdml rdml = new rdml(); rdml.setrdmlid(id); while (workproducts.hasnext()) { workproduct = workproducts.nextnode(); string rdmlid = workproduct.getproperty("jcr:id").getvalue() .getstring(); if (rdmlid.equals(id)) { rdml.setrdmlasnode(workproduct); document doc = convertnodetodoc(workproduct); rdml.setrdmlasdocument(doc); break; } } // rdml 반환 return rdml; } // retrieverecent 반환값 rdml로 바꾼 후 수정 필요 @override public arraylist<string> retrieverdmlhistory(string id) throws revisioncontrolexception, exception { // todo auto-generated method stub arraylist<string> histroyresult = new arraylist<string>(); node curnode = convertdoctonode(retrieverecentrdml(id) .getrdmlasdocument(), id); versionhistory history = session.getworkspace().getversionmanager() .getversionhistory(curnode.getpath()); versioniterator versioniterator = history.getallversions(); while (versioniterator.hasnext()) { version version = (version) versioniterator.next(); string = "version found : " + version.getname() + " - " + version.getnode(version.getpath()).getname() + " - " + version.getcreated().gettime(); histroyresult.add(a); } return histroyresult; } public rdml retrieverdml(string keyword) throws rdmldbexception, repositoryexception, exception { string sql = "select * [nt:base] [jcr:content] = '"+keyword+"'"; query query = querymanager.createquery(sql, query.jcr_sql2); queryresult result = query.execute(); nodeiterator nodes = result.getnodes(); while(nodes.hasnext()){ node node = nodes.nextnode(); if(node.getproperty("jcr:id")!=null) system.out.println(node.getproperty("jcr:id").getstring()); } return null; } private string extractcontent(element element) throws exception { string content = ""; content = element.getvalue(); return content; } private list<element> extractworkproductitems(document rd) { element workproduct = rd.getrootelement(); list<element> workproductitems = workproduct .getchildren("workproductitem"); return workproductitems; } private void initialversioning(node workproduct) throws exception { workproduct.addmixin("mix:versionable"); nodeiterator workproductitems = workproduct.getnodes("workproductitem"); while (workproductitems.hasnext()) { node workproductitem = workproductitems.nextnode(); workproductitem.addmixin("mix:versionable"); } session.save(); } private void versionup() throws exception { nodeiterator workproductitems = inputedwp.getnodes("workproductitem"); if (workproductitems == null) { workproductversionup(); } else { workproductitemversionup(); } } // if workproductitem not exist, compare base workproduct's contents , // inputed workproduct's contents. change version. private void workproductversionup() throws repositoryexception { string base = basewp.getproperty("jcr:content").getvalue().getstring(); string input = inputedwp.getproperty("jcr:content").getvalue() .getstring(); if (base.equals(input)) { inputedwp.remove(); system.out.println("recent workproduct version persist"); } else { // ��� if (basewp.getproperty("jcr:deleted").getvalue().getstring() .equals("false")) { vm.checkout(basewp.getpath()); basewp.getproperty("jcr:content").setvalue(input); session.save(); vm.checkin(basewp.getpath()); inputedwp.remove(); system.out.println("workproduct version changed"); } else { system.out.println("this workproduct has deleted"); } } } private void workproductitemversionup() throws exception { setallbaseworkproductitemid(); setallinputworkproductitemid(); vm.checkout(basewp.getpath()); nodeiterator inputwpis = inputedwp.getnodes("workproductitem"); while (inputwpis.hasnext()) { node inputwpi = inputwpis.nextnode(); // inputed workproductitem's id string inputwpiid = inputwpi.getproperty("jcr:id").getvalue() .getstring(); // inputed workproductitem's contents string inputwpicontent = inputwpi.getproperty("jcr:content") .getvalue().getstring(); // base workproductitem same id node basewpi = getbaseworkproductitem(inputwpiid, "workproductitem"); // compare base workproductitem , inputed workproductitem if (basewpi != null) { // base workproductitem's contents string basewpicontent = basewpi.getproperty("jcr:content") .getvalue().getstring(); // compare workproductitems contents. if (!isdeleted(basewpi)) { if (!inputwpicontent.equals(basewpicontent)) { vm.checkout(basewpi.getpath()); basewpi.getproperty("jcr:content").setvalue( inputwpicontent); session.save(); vm.checkin(basewpi.getpath()); system.out.println(inputwpiid + "is changed"); } } else { vm.checkout(basewpi.getpath()); basewpi.getproperty("jcr:deleted").setvalue("false"); if (!inputwpicontent.equals(basewpicontent)) { basewpi.getproperty("jcr:content").setvalue( inputwpicontent); system.out.println(inputwpiid + "is undeleted , changed"); } else { system.out.println(inputwpiid + "is undeleted"); } session.save(); vm.checkin(basewpi.getpath()); } } } addnodes("workproductitem"); deletenodes("workproductitem"); inputedwp.remove(); setallbaseworkproductitemid(); session.save(); vm.checkin(basewp.getpath()); system.out.println(basewp.getpath() + " version = " + vm.getbaseversion(basewp.getpath())); } private node getbaseworkproductitem(string id, string nodetype) throws exception { nodeiterator childnodes = basewp.getnodes(nodetype); node childnode = null; while (childnodes.hasnext()) { childnode = childnodes.nextnode(); string childid = childnode.getproperty("jcr:id").getvalue() .getstring(); if (id.equals(childid)) { remainedbaseworkproductitemids.remove(childid); remainedinputedworkproductitemids.remove(childid); return childnode; } } return null; } private void setallbaseworkproductitemid() throws repositoryexception { nodeiterator basewpis = basewp.getnodes("workproductitem"); remainedbaseworkproductitemids.clear(); while (basewpis.hasnext()) { node basewpi = basewpis.nextnode(); remainedbaseworkproductitemids.add(basewpi.getproperty("jcr:id") .getvalue().getstring()); } } private void setallinputworkproductitemid() throws repositoryexception { nodeiterator inputedwpis = inputedwp.getnodes("workproductitem"); remainedinputedworkproductitemids.clear(); while (inputedwpis.hasnext()) { node inputedwpi = inputedwpis.nextnode(); remainedinputedworkproductitemids.add(inputedwpi .getproperty("jcr:id").getvalue().getstring()); } } private boolean isdeleted(node node) throws exception { boolean isdeleted = node.getproperty("jcr:deleted").getvalue() .getstring().equals("false"); if (isdeleted) { return false; } else { return true; } } private void addnodes(string nodetype) throws exception { iterator<string> iter = remainedinputedworkproductitemids.iterator(); while (iter.hasnext()) { string wpiid = iter.next(); nodeiterator nodes = inputedwp.getnodes(nodetype); node node = null; boolean ismatch = false; while (nodes.hasnext()) { node = nodes.nextnode(); string nodeid = node.getproperty("jcr:id").getvalue() .getstring(); if (nodeid.equals(wpiid)) { ismatch = true; break; } } if (ismatch) { string nodeid = node.getproperty("jcr:id").getvalue() .getstring(); string nodecontent = node.getproperty("jcr:content").getvalue() .getstring(); if (!isdeleted(node)) { node newnode = basewp.addnode(nodetype); newnode.setproperty("jcr:id", nodeid); newnode.setproperty("jcr:content", nodecontent); newnode.setproperty("jcr:deleted", "false"); newnode.addmixin("mix:versionable"); } system.out.println(nodeid + "is added"); } } } private void deletenodes(string nodetype) throws exception { iterator<string> iter = remainedbaseworkproductitemids.iterator(); while (iter.hasnext()) { string wpiid = iter.next(); nodeiterator nodes = basewp.getnodes(nodetype); node node = null; boolean ismatch = false; while (nodes.hasnext()) { node = nodes.nextnode(); string nodeid = node.getproperty("jcr:id").getvalue() .getstring(); if (nodeid.equals(wpiid)) { ismatch = true; break; } } if (ismatch) { deletenode(node); system.out.println(node.getproperty("jcr:id").getvalue() .getstring() + "is deleted"); } } } private boolean deletenode(node node) throws exception { vm.checkout(node.getpath()); node.getproperty("jcr:deleted").setvalue("true"); session.save(); vm.checkin(node.getpath()); return true; } private document convertnodetodoc(node workproduct) throws valueformatexception, illegalstateexception, pathnotfoundexception, repositoryexception, ioexception { element rootelement = new element("workproduct"); document doc = new document(rootelement); // doc.setrootelement(rootelement); rootelement.setattribute(new attribute("id", workproduct .getproperty("jcr:id").getvalue().getstring())); nodeiterator workprouctitems = workproduct.getnodes(); while (workprouctitems.hasnext()) { node workprocutitemnode = workprouctitems.nextnode(); element workproductitem = new element("workproductitem"); workproductitem.addcontent(new element("content") .settext(workprocutitemnode.getproperty("jcr:content") .getvalue().tostring())); rootelement.addcontent(workproductitem); } xmloutputter xmloutput = new xmloutputter(); xmloutput.output(doc, new filewriter(new file("resource/output_rdml/example_output" + string.valueof(++docnumber) + ".xml"))); return doc; } private node convertdoctonode(document workproduct, string id) throws illegalstateexception, exception { if (workproduct == null) return null; node node = root.addnode("workproduct"); node.setproperty("jcr:id", id); node.setproperty("jcr:deleted", false); list<element> docworkproductitems = extractworkproductitems(workproduct); (int = 0; < docworkproductitems.size(); i++) { element docworkproductitem = docworkproductitems.get(i); string workproductitemid = docworkproductitem .getattributevalue("id"); node nodeworkproductitem = node.addnode("workproductitem"); nodeworkproductitem.setproperty("jcr:deleted", false); nodeworkproductitem.setproperty("jcr:id", workproductitemid); nodeworkproductitem.setproperty("jcr:content", extractcontent(docworkproductitems.get(i))); } return node; } public void sessionlogout() { session.logout(); } }
this test code.
package kr.ac.jbnu.sql.soremore.service.rdml; import java.io.file; import java.io.ioexception; import java.util.arraylist; import kr.ac.jbnu.sql.soremore.model.rdml; import kr.ac.jbnu.sql.soremore.service.irdmldbmgmt; import kr.ac.jbnu.sql.soremore.service.rdmldbexception; import org.jdom2.document; import org.jdom2.jdomexception; import org.jdom2.input.saxbuilder; import org.junit.assert; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.abstractjunit4springcontexttests; import org.springframework.test.context.junit4.springjunit4classrunner; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "classpath*:soremore-servlet.xml" }) public class rdmldbmgmttest extends abstractjunit4springcontexttests { @autowired irdmldbmgmt rdmldbmgmt = null; @test public void storerdml() { arraylist<rdml> rdmls = loadtestrdml(); boolean issuccessful = false; (rdml rdml : rdmls) { try { issuccessful = rdmldbmgmt.storerdml(rdml); system.out.println(); } catch (rdmldbexception e) { e.printstacktrace(); issuccessful = false; } catch (exception e) { e.printstacktrace(); issuccessful = false; } } assert.asserttrue(issuccessful); } @test public void queryrdml0() { string[] rdmlids = { "rd01", "rd02" }; boolean issuccessful = false; (string rdmlid : rdmlids) { try { issuccessful = false; rdml rdml = rdmldbmgmt.retrieverecentrdml(rdmlid); if (rdml.getrdmlasdocument() != null) { document doc = rdml.getrdmlasdocument(); if (doc != null) { issuccessful = true; } } assert.asserttrue(issuccessful); } catch (rdmldbexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } } @test public void queryrdml1(){ string[] keywords = { "연구", "rd" }; boolean issuccessful = false; (string keyword : keywords) { try { issuccessful = false; rdml rdml = rdmldbmgmt.retrieverdml(keyword); if (rdml.getrdmlasdocument() != null) { document doc = rdml.getrdmlasdocument(); if (doc != null) { issuccessful = true; } } assert.asserttrue(issuccessful); } catch (rdmldbexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } } public arraylist<rdml> loadtestrdml() { arraylist<rdml> rdmls = new arraylist<rdml>(); string xmlsource = "resource/sample_rdml"; file sourcedir = new file(xmlsource); file[] sourcefiles = sourcedir.listfiles(); (file file : sourcefiles) { rdml rdml = new rdml(); rdml.setrdmlasdocument(createdocument(file)); rdmls.add(rdml); } return rdmls; } private document createdocument(file rdmlpathasfile) { saxbuilder jdombuilder = new saxbuilder(); document jdomdocument = null; try { jdomdocument = jdombuilder.build(rdmlpathasfile); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return jdomdocument; } }
finally error stack.
org.springframework.beans.factory.beancreationexception: error creating bean name 'kr.ac.jbnu.sql.soremore.service.rdml.rdmldbmgmttest': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: kr.ac.jbnu.sql.soremore.service.irdmldbmgmt kr.ac.jbnu.sql.soremore.service.rdml.rdmldbmgmttest.rdmldbmgmt; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kr.ac.jbnu.sql.soremore.service.irdmldbmgmt] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1204) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowirebeanproperties(abstractautowirecapablebeanfactory.java:385) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:117) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:82) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:212) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:199) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:251) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:253) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:216) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:82) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:60) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:67) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:162) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: kr.ac.jbnu.sql.soremore.service.irdmldbmgmt kr.ac.jbnu.sql.soremore.service.rdml.rdmldbmgmttest.rdmldbmgmt; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kr.ac.jbnu.sql.soremore.service.irdmldbmgmt] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:555) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:331) ... 26 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kr.ac.jbnu.sql.soremore.service.irdmldbmgmt] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1261) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1009) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:904) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:527) ... 28 more
the problem see code , have created bean twice implementation rdmldbmgmtimpl
. once xml mapping :
<bean id="rdmldbmgmt" class="kr.ac.jbnu.sql.soremore.service.rdml.rdmldbmgmtimpl" />
and second, have used @service
annotation on class rdmldbmgmtimpl
.
remove bean mapping xml or remove annotation. add context:annotation-config
making @autowired
annotation work.
cannot add comment because of low reputation points.
Comments
Post a Comment