SpringJUnit4ClassRunner加上Log4j2加上Log4j2.xml在类路径之外

SpringJUnit4ClassRunner加上Log4j2加上Log4j2.xml在类路径之外,spring,log4j2,Spring,Log4j2,我有一些测试是用SpringJUnit4ClassRunner运行的。我也刚迁移到Log4j2,现在加载我的Log4j2.xml配置文件时遇到问题,我总是遇到以下错误: ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 问题是配置文件不在类路径上。相反,我将它与类路径之外的其他配置数据一起保存

我有一些测试是用
SpringJUnit4ClassRunner
运行的。我也刚迁移到Log4j2,现在加载我的
Log4j2.xml
配置文件时遇到问题,我总是遇到以下错误:

ERROR StatusLogger No log4j2 configuration file found. Using default 
      configuration: logging only errors to the console.
问题是配置文件不在类路径上。相反,我将它与类路径之外的其他配置数据一起保存在一个单独的文件夹中。当我开始测试时,我已经设置了
-Dconfig=/path/to/config/folder
。我知道我在运行测试时也可以通过
-Dlog4j.configurationFile=…
,但我不想这样做

我想做的是使用基于Spring注释的配置来编写一个配置类,在该类中我通过编程设置了Log4j的位置。如果我有,这是可能的,但我不知道如何将其应用于
SpringJUnit4ClassRunner

@Configuration
public class Log4jConfiguration {
    /** Initialize Log4j2 with a custom location for the log4j2.xml **/
}

可能是您的Maven,请指定:

<resources>
    <resource>
        <directory>${basedir}/src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include><!---->
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

${basedir}/src/main/java
**/*.xml
假的
${basedir}/src/main/resources
**/*.物业
**/*.xml
假的

检查它。

使用
-Dlog4j.configurationFile=…
与使用
-Dconfig=/path/to/config/folder
实现的目标完全相同。那么,为什么不使用第一个选项并依靠自动配置呢?你真的不需要在这个问题上重新设计轮子,因为我在那个文件夹中有其他配置文件,而且我的代码中有很多地方都依赖它,所以我不想更改它。