从test / resources注入spring bean无法从main / java中找到类

时间:2014-01-09 03:24:00

标签: java spring maven dependency-injection

我在maven项目中使用xml定义了一个spring bean。它在src / main / java中使用pojo类。此xml文件当前存储在src / main / resources中。构建时,一切正常,它作为资源注入。

   <bean id="test" class="org.testing.framework.model.TestModel">
      values here
   </bean>

但我想将xml文件移动到src / test / resources,但仍然将它从src / main / java引用pojo作为其测试数据。当我将其移动到测试时会抛出错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel]

是否可以将xml spring bean文件移动到test并使用main,如果是这样的话?

由于

的applicationContext.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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd">

    <import resource="classpath*:TestBeans.xml" />

</beans>

TestBeans.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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="test" class="org.testing.framework.model.TestModel">
       values....
    </bean>

    <util:map id="testMap" value-type="org.testing.framework.model.TestModel">
        <entry key="testKey" value-ref="test" />
    </util:map>

</beans>

Steps.java

package org.testing.framework.steps;

import java.util.Map;
import javax.annotation.Resource;
import net.thucydides.core.pages.Pages;
import org.springframework.stereotype.Component;
import org.testing.framework.model.TestModel;
import org.testing.framework.pages.XmlFilePath;

@Component
public class Steps extends AuatSteps {

    @Resource
    private XmlFilePath testXmlFilePath;

    @Resource
    private Map<String, TestModel> testMap;

    public Steps(Pages pages) {
        super(pages);
    }

    public String getXmlFilePath() {
        return testXmlFilePath.getXmlFilePath();
    }

    public Map<String, TestModel> getTestMap() {
        return testMap;
    }
}

堆栈追踪:

1069 [main] ERROR org.springframework.test.context.TestContextManager  - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@233dcd27] to prepare test instance [WoWSteps$$EnhancerByCGLIB$$f625fb6]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.testing.framework.steps.Steps': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel] found for dependency [map with value type org.testing.framework.model.TestModel]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at net.thucydides.core.steps.SpringDependencyInjector.injectDependenciesInto(SpringDependencyInjector.java:16)
    at net.thucydides.core.steps.StepFactory.injectOtherDependenciesInto(StepFactory.java:124)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:114)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:100)
    at net.thucydides.core.steps.StepFactory.getStepLibraryFor(StepFactory.java:66)
    at net.thucydides.core.steps.StepAnnotations.instantiateAnyUnitiaializedSteps(StepAnnotations.java:50)
    at net.thucydides.core.steps.StepAnnotations.instanciateScenarioStepFields(StepAnnotations.java:41)
    at net.thucydides.core.steps.StepAnnotations.injectScenarioStepsInto(StepAnnotations.java:23)
    at net.thucydides.jbehave.ThucydidesStepFactory.createInstanceOfType(ThucydidesStepFactory.java:80)
    at org.jbehave.core.steps.StepCandidate.getStepsInstance(StepCandidate.java:68)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addRegularStep(JUnitDescriptionGenerator.java:190)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addExistingStep(JUnitDescriptionGenerator.java:148)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addSteps(JUnitDescriptionGenerator.java:138)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addStepsToExample(JUnitDescriptionGenerator.java:127)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.insertDescriptionForExamples(JUnitDescriptionGenerator.java:121)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.createDescriptionFrom(JUnitDescriptionGenerator.java:55)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addAllScenariosToDescription(JUnitDescriptionGenerator.java:208)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.createDescriptionFrom(JUnitDescriptionGenerator.java:44)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.addStories(ThucydidesReportingRunner.java:265)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.buildDescriptionFromStories(ThucydidesReportingRunner.java:245)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.getDescriptions(ThucydidesReportingRunner.java:74)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.getDescription(ThucydidesReportingRunner.java:120)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.run(ThucydidesReportingRunner.java:145)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel] found for dependency [map with value type org.testing.framework.model.TestModel]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:824)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 40 more
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.633 sec <<< FAILURE!

1 个答案:

答案 0 :(得分:0)

所以在玩完之后我解决了这个问题。

TestBeans.xml文件放在src / test / resources中名为applicationContext的子文件夹中。

以下行从src / main / resources中的appliationContext.xml中选择bean完全没法

<import resource="classpath*:TestBeans.xml" />

但是这个bean无法访问pojo org.testing.framework.model.TestModel

现在我将导入更改为

<import resource="classpath*:applicationContext/TestBeans.xml" />

它可以像原始线一样拾取豆子,但是bean可以访问pojo。我不清楚为什么它需要文件夹名称,因为文件被正常拾取。它只是无法访问没有文件夹名称的pojo。

有谁知道为什么会这样?无论如何,谢谢你看。