【问题标题】:injecting beans in Spring MvC when running a test运行测试时在 Spring MvC 中注入 bean
【发布时间】:2020-10-04 22:15:58
【问题描述】:

我在运行测试时遇到此错误:

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonanza.api.IWorkflowService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这是我在运行类时加载的 servlet-xml:

  <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jpa="http://www.springframework.org/schema/data/jpa"
            xmlns:util="http://www.springframework.org/schema/util"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bonanza.*" />
        <jpa:repositories base-package="com.bonanza.*" />
        <!-- transaction management -->
        <mvc:annotation-driven />
    
        <bean id="workflowService"
                class="com.bonanza.api.IWorkflowService" abstract="true"/>
..
</beans>

【问题讨论】:

  • 这是您正在更新的旧项目还是正在创建的新项目?
  • 遗留项目
  • @NunyetdeCanCalçada 你能提供你的Java类吗?如果你使用的是@Autowired?您的 XML 文件看起来不错,如当前所述。
  • 据我所知,标记为“抽象”的 bean 没有被 Spring 实例化。通常抽象 bean 用于对子 bean 的公共属性进行分组并减少 XML 代码。第一步是在 XML 配置中删除 abstract="true" 并检查接下来会显示哪个错误。另外,com.bonanza.api.IWorkflowService 似乎是一个接口(根据它的名称),实际上 XML 中的 bean 定义应该指向一个接口实现。我同意,看看 IWorkflowService 声明会很有用。

标签: java spring spring-mvc dependency-injection inversion-of-control


【解决方案1】:

我认为您的问题是您正在尝试使用接口而不是具体类来注入 com.bonanza.api.IWorkflowService 类型的依赖项,您的应用程序中需要其他 bean。

为了解决这个问题,你有几个选择。

一方面,您当然可以为该接口提供一个实际的实现。

或者,另一方面,您可以为该接口提供一个模拟对象。您可以为此使用Mockito,并使用类似于以下代码的内容修改您的 Spring XML 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jpa="http://www.springframework.org/schema/data/jpa"
            xmlns:util="http://www.springframework.org/schema/util"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bonanza.*" />
        <jpa:repositories base-package="com.bonanza.*" />
        <!-- transaction management -->
        <mvc:annotation-driven />
    
        <bean id="workflowService" class="org.mockito.Mockito" factory-method="mock">
            <constructor-arg value="com.bonanza.api.IWorkflowService" />
        </bean>
..
</beans>

如果需要,您可以在配置中自定义模拟行为(来自您的other question):

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:backoffice-servlet.xml")
public class TimeControllerTests {

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private IWorkflowService workflowService;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        // given(this.workflowService...).willReturn(...);
    }
    
    @Test
    public void should_OK() throws Exception {

        mockMvc.perform(get("/time/2")
                .contentType(APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2015-03-04
    • 2016-08-08
    • 2012-07-31
    相关资源
    最近更新 更多