Java Spring remove打印输出无法从URL加载属性
我正在构建一个gradle插件,我想让用户能够覆盖外部文件中的一些属性。 我使用SpringPropertyPlaceHolderConfigure来设置一些用户可以放置文件的目的地,这是可以预期的。 我的问题是,当运行我的插件时,Spring打印输出到控制台:“无法从URL[…]加载属性” 有人知道我会如何压制这条信息吗 我尝试过覆盖logback和sl4j,但没有成功 我的Spring应用程序上下文xml:Java Spring remove打印输出无法从URL加载属性,java,spring,gradle,gradle-plugin,Java,Spring,Gradle,Gradle Plugin,我正在构建一个gradle插件,我想让用户能够覆盖外部文件中的一些属性。 我使用SpringPropertyPlaceHolderConfigure来设置一些用户可以放置文件的目的地,这是可以预期的。 我的问题是,当运行我的插件时,Spring打印输出到控制台:“无法从URL[…]加载属性” 有人知道我会如何压制这条信息吗 我尝试过覆盖logback和sl4j,但没有成功 我的Spring应用程序上下文xml: <?xml version="1.0" encoding="UTF-8"?&g
<?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>
类路径:/conf/plugin.properties
文件:${user.home}/conf/plugin.properties
文件:${user.home}/conf/plugin-override.properties
据我所知,没有此类文件
尝试将下面的配置添加到属性PlaceHolderConfiguration
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
如果要实现自定义逻辑来设置位置,可以扩展
PropertyPlaceHolderConfigure
如
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任务中的记录器级别设置为Error,如下所示:
task.doFirst {
logger.context.level = LogLevel.ERROR
logging.captureStandardOutput LogLevel.ERROR
}
你是对的,文件不存在,忽略属性通过没有失败的构建来工作,但是,遗憾的是,调试消息由于某种原因仍然被打印出来。这很有可能会工作,但对我来说,这有点过分,所以我最终使用了你的解决方案,尽管我需要覆盖的函数已经设置好了