上下文:组件扫描不适用于junit

时间:2019-02-26 07:11:53

标签: java spring-mvc junit

我在applicationContext.xml中添加了所有基本软件包,但仍显示noBeanDefinitionException

  

applicationContext.xml

  <!-- Autowiring -->

            <context:component-scan base-package="com.paymentech.developercenter.services"/>

        <import resource="classpath:spring/applicationContext-fragment-mybatisMOCK.xml" />
  

DispathcherServlet-servlet.xml

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

      <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
        <property name="defaultLocale" value="en_US" />
    </bean>
    <bean class="org.springframework.web.servlet.DispatcherServlet"/>
  

FaqControllerTest.java

@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:spring/DispatcherServlet-servlet.xml" })
    @WebAppConfiguration("WEB-INF/web.xml")

    public class FaqControllerTest {
        @Autowired
        private WebApplicationContext context;

        @Autowired
        private DispatcherServlet dispatcherServlet;

        @Before
        public void init() throws ServletException, IOException {
            SpringTestConfiguration.init(context, dispatcherServlet);

        }
  

错误:-

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'integratorSolutionController': Unsatisfied dependency expressed through field 'solutionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'integratorSolutionService': Unsatisfied dependency expressed through field 'solutionStatusService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solutionStatusService': Unsatisfied dependency expressed through field 'solutionStatusMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.paymentech.developercenter.persistence.isg.mappers.SolutionStatusMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

1 个答案:

答案 0 :(得分:0)

您的错误:

  

由以下原因引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ integratorSolutionController”的bean时出错:通过字段“ solutionService”表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ integratorSolutionService”的bean时出错:通过字段“ solutionStatusService”表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'solutionStatusService'的bean时出错:通过字段'solutionStatusMapper'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'com.paymentech.developercenter.persistence.isg.mappers.SolutionStatusMapper'的合格Bean:应该至少有1个可以作为自动装配候选的Bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

这意味着,Spring无法创建名称为“ solutionStatusService”的对象。

为什么:因为Spring无法创建其依赖项之一,名为“ solutionStatusMapper”。

根本原因:您未添加组件扫描,该组件扫描将扫描以“ com.paymentech.developercenter.persistence”表示的持久层。 OR 它,并且您没有在“ solutionStatusMapper”上方添加@Component。否则,Spring会看到它。