gpt4 book ai didi

SpringRunner 忽略应用程序属性

转载 作者:行者123 更新时间:2023-12-04 20:30:00 25 4
gpt4 key购买 nike

我正在尝试按如下方式创建 Spring 集成测试:

@RunWith(SpringRunner::class)
@ActiveProfiles(profiles = ["Test"])
@ContextConfiguration(locations = ["classpath:**/applicationContext.xml"])
open class SimpleEntityIT {...}

applicationContact.xml 包含:
    <context:annotation-config/>
<context:spring-configured/>
<context:property-placeholder
ignore-resource-not-found="false"
location="classpath:application${spring.profiles.active}.properties,classpath:application.properties"/>

<context:component-scan base-package="net.goout"/>

applicationContext 已加载,但似乎大部分都被忽略了。 Bean 不是通过组件扫描和 application.properties 构造的。完全被忽略,日志中没有提及:
 2018-01-26 20:09:26,131 DEBUG Resolved location pattern [classpath:**/applicationContext.xml] to resources []
2018-01-26 20:09:26,132 DEBUG Loaded 0 bean definitions from location pattern [classpath:**/applicationContext.xml]
2018-01-26 20:09:26,167 INFO Refreshing org.springframework.context.support.GenericApplicationContext@aecb35a: startup date [Fri Jan 26 20:09:26 CET 2018]; root of context hierarchy
2018-01-26 20:09:26,167 DEBUG Bean factory for org.springframework.context.support.GenericApplicationContext@aecb35a: org.springframework.beans.factory.support.DefaultListableBeanFactory@20d3d15a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory]; root of factory hierarchy
2018-01-26 20:09:26,198 DEBUG Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2018-01-26 20:09:26,198 DEBUG Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2018-01-26 20:09:26,225 DEBUG Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
2018-01-26 20:09:26,231 DEBUG Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'

我没有得到什么?

编辑:不是事件组件扫描 bean 没有被构造,但实际上没有 bean 被构造——即使是那些由 <bean> 定义的bean在 applicationContext.xml 中。即使它被正确找到,它的内容似乎也被忽略了。

最佳答案

根据最新评论,我相信这个问题已经解决,但即便如此,还是有一些快速观察和替代方案:

a) 您没有收到关于缺少资源的错误是因为 attr 以下,我建议尽早确定问题:

忽略资源未找到=“假”

此外,您可以在下面使用:

@PropertySource(value = "xyz.properties", ignoreResourceNotFound = true)

b) 完全无关,但:
@ActiveProfiles(profiles = ["Test"])

可以写成:
@ActiveProfiles({"Test","QA"}) 

or in your application-test.properties

spring.profiles.active=Test,QA

c) 为了放置 applicationContext.xml ,也可以在 web.xml 中放置替代方法:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*.xml</param-value>
</context-param>

(虽然,使用@ContextConfiguration 指定确切位置的注释也完全没问题!)

最重要的是,如果您使用基于注释的配置,我会建议使用 Spring Boot :-)

希望能帮助到你!!

关于SpringRunner 忽略应用程序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48468156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com