Spring remove printout Could not load properties f

2019-08-26 08:45发布

I'm building a gradle plugin in which I want to give users the ability to override some properties from an external file. I'm using the Spring propertyPlaceholderConfigurer in order to set a few destinations where users can place their files, This works as expected. My problem is that when running my plugin Spring prints out to the console: "Could not load properties from URL [...]" Does anyone have any idea how I would go about squelching this message?

I've tried overriding logback and sl4j to no avail.

My Spring application-context 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>

3条回答
forever°为你锁心
2楼-- · 2019-08-26 09:11

If you want to implement custom logic to set locations you can extend PropertyPlaceholderConfigurer like

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));
    }

and use it like

<bean id="propertyPlaceholderConfigurer" class="package.CustomPropertyPlaceholderConfigurer ">
    <property name="placeholderPrefix" value="${"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesMode" value="2"/>
</bean>
查看更多
叼着烟拽天下
3楼-- · 2019-08-26 09:13

As far as I understand there are no such files.

Try to add configs below to the propertyPlaceholderConfigurer

<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
查看更多
一纸荒年 Trace。
4楼-- · 2019-08-26 09:29

I found that the message was originating in PropertiesLoaderSupport at the loadProperties method:

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

So what I ended up doing is set the logger level in my gradle task to Error as follows:

task.doFirst {
    logger.context.level = LogLevel.ERROR
    logging.captureStandardOutput LogLevel.ERROR
}
查看更多
登录 后发表回答