我正在构建一个gradle插件,我想让用户能够覆盖外部文件中的某些属性。 我正在使用Spring propertyPlaceholderConfigurer来设置用户可以放置文件的几个目的地,这可以按预期工作。 我的问题是,在运行我的插件时,Spring打印到控制台:“无法从URL [...]加载属性” 有谁知道如何压制这条消息?
我已经尝试重写logback和sl4j无济于事。
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>
答案 0 :(得分:1)
如果要实现自定义逻辑来设置位置,可以像
一样扩展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>
答案 1 :(得分:0)
据我所知,没有这样的文件。
尝试在propertyPlaceholderConfigurer
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
答案 2 :(得分:0)
我发现该消息源自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
}