Spring 删除打印输出无法从 URL 加载属性

Spring remove printout Could not load properties from URL

我正在构建一个 gradle 插件,我想让用户能够在其中覆盖外部文件的某些属性。 我正在使用 Spring propertyPlaceholderConfigurer 来设置一些用户可以放置文件的目的地,这按预期工作。 我的问题是,当 运行 我的插件 Spring 打印到控制台时:"Could not load properties from URL [...]" 有谁知道我将如何压制这条消息?

我试过覆盖 logback 和 sl4j 但无济于事。

我的 Spring 应用程序上下文 xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"
   default-lazy-init="true">

<!-- Activates scanning of @Autowired -->
<context:annotation-config/>

<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.example.test"/>

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/conf/plugin.properties</value>
            <value>file:${user.home}/conf/plugin.properties</value>
            <value>file:${user.home}/conf/plugin-override.properties</value>
        </list>
    </property>
    <property name="placeholderPrefix" value="${"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesMode" value="2"/>
</bean>

据我所知,没有这样的文件。

尝试将以下配置添加到 propertyPlaceholderConfigurer

<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />

如果您想实施自定义逻辑来设置位置,您可以像

一样扩展PropertyPlaceholderConfigurer
class CustomPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {

    public void setFileName(String fileName) throws FileNotFoundException {
        File file = findPropertyFile(fileName);  // implement property file loading
        super.setLocation(new FileSystemResource(file));
    }

并像

一样使用它
<bean id="propertyPlaceholderConfigurer" class="package.CustomPropertyPlaceholderConfigurer ">
    <property name="placeholderPrefix" value="${"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesMode" value="2"/>
</bean>

我发现该消息源自 PropertiesLoaderSupport 在 loadProperties 方法中:

if (this.ignoreResourceNotFound) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
                    }
                }

所以我最终做的是将 gradle 任务中的记录器级别设置为错误,如下所示:

task.doFirst {
    logger.context.level = LogLevel.ERROR
    logging.captureStandardOutput LogLevel.ERROR
}