如何解决 "injection of autowired dependencies failed" 问题?
how to solve "injection of autowired dependencies failed" issue?
我尝试开发一些服务器程序。所以,我使用 junit4 测试程序。
但是突然注入自动装配的依赖项失败错误发生。
我不知道如何解决这个问题。请帮助我。
这是上下文-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>
这是界面。
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;
}
这是接口的实现代码
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 a = "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 * from [nt:base] where [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 is not exist, compare base workproduct's contents and
// inputed workproduct's contents. Then change the 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 is 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();
// get inputed workproductItem's id
String inputWPIid = inputWPI.getProperty("jcr:id").getValue()
.getString();
// get inputed workproductItem's contents
String inputWPIcontent = inputWPI.getProperty("jcr:content")
.getValue().getString();
// get base workproductItem with same id
Node baseWPI = getBaseWorkProductItem(inputWPIid, "WorkProductItem");
// compare base workproductItem and inputed workproductItem
if (baseWPI != null) {
// get 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 and 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);
for (int i = 0; i < 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();
}
}
这是测试代码。
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;
for (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;
for (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;
for (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();
for (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;
}
}
最后这是错误堆栈。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest.rdmlDBMgmt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:251)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest.rdmlDBMgmt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at 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 for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
... 28 more
从代码中看到的问题是,您为实现创建了两次 bean RDMLDBMgmtImpl
。一旦 XML 映射为 :
<bean id="RDMLDBMgmt" class="kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtImpl" />
其次,您在 class RDMLDBMgmtImpl
.
上使用了 @Service
注释
从 XML 中删除 bean 映射或删除注释。还要添加 context:annotation-config
以使 @Autowired
注释起作用。
由于信誉点数低,无法添加评论。
我尝试开发一些服务器程序。所以,我使用 junit4 测试程序。 但是突然注入自动装配的依赖项失败错误发生。 我不知道如何解决这个问题。请帮助我。
这是上下文-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>
这是界面。
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;
}
这是接口的实现代码
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 a = "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 * from [nt:base] where [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 is not exist, compare base workproduct's contents and
// inputed workproduct's contents. Then change the 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 is 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();
// get inputed workproductItem's id
String inputWPIid = inputWPI.getProperty("jcr:id").getValue()
.getString();
// get inputed workproductItem's contents
String inputWPIcontent = inputWPI.getProperty("jcr:content")
.getValue().getString();
// get base workproductItem with same id
Node baseWPI = getBaseWorkProductItem(inputWPIid, "WorkProductItem");
// compare base workproductItem and inputed workproductItem
if (baseWPI != null) {
// get 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 and 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);
for (int i = 0; i < 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();
}
}
这是测试代码。
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;
for (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;
for (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;
for (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();
for (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;
}
}
最后这是错误堆栈。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest.rdmlDBMgmt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:251)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtTest.rdmlDBMgmt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [kr.ac.jbnu.sql.soremore.service.IRDMLDBMgmt] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at 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 for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
... 28 more
从代码中看到的问题是,您为实现创建了两次 bean RDMLDBMgmtImpl
。一旦 XML 映射为 :
<bean id="RDMLDBMgmt" class="kr.ac.jbnu.sql.soremore.service.rdml.RDMLDBMgmtImpl" />
其次,您在 class RDMLDBMgmtImpl
.
@Service
注释
从 XML 中删除 bean 映射或删除注释。还要添加 context:annotation-config
以使 @Autowired
注释起作用。
由于信誉点数低,无法添加评论。