Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring 3+;。按占位符中的前缀解析属性_Java_Spring - Fatal编程技术网

Java Spring 3+;。按占位符中的前缀解析属性

Java Spring 3+;。按占位符中的前缀解析属性,java,spring,Java,Spring,在我的Java类中,我有一个类型为Java.util.Properties或Java.util.Map的字段。 我希望Spring向字段中注入以给定前缀开头的所有属性。理想情况下,通过在属性注释中指定通配符,smth类似于@Value(${prefix.*}),但不幸的是,它似乎不起作用 我怎样才能做到这一点呢?好吧,没有直接的方法可以实现你想要的 但是,您可以将springel与org.springframework.core.io.support.PropertiesLoaderSuppor

在我的Java类中,我有一个类型为Java.util.Properties或Java.util.Map的字段。 我希望Spring向字段中注入以给定前缀开头的所有属性。理想情况下,通过在属性注释中指定通配符,smth类似于
@Value(${prefix.*})
,但不幸的是,它似乎不起作用


我怎样才能做到这一点呢?

好吧,没有直接的方法可以实现你想要的

但是,您可以将
springel
org.springframework.core.io.support.PropertiesLoaderSupport
的自定义实现结合使用

首先扩展(或其任何子类)。引入一个方法(比如filterProps),它将返回带有过滤属性的
java.util.Properties
对象,如下所示

public class CustomPropertyLoader extends PropertiesLoaderSupport {

public Properties filterProperties(String prefix){
    Properties temp = new Properties();

    for(Properties props : this.localProperties){
        /* 
        * Iterate over props and filter them as 
        * per prefix (or any custom logic) to temp
        */
    }
    return temp;
}
}

提示-对于更一般的场景,您可以使用正则表达式,而不是将
String
用作方法参数

Second定义上述类的对应bean

<bean id="customPropertyLoader" class="x.y.z.CustomPropertyLoader">
    <property name="locations" value="classpath*:/**/some*.properties"/>
</bean>

Last将类中
java.util.Properties
类型的字段注释为
@Value(#{customPropertyLoader.filterProperties('prefix.*')})


注意:请原谅任何编译/语法错误,因为我还没有编译并检查设置;但它应该起作用。如果没有,请在注释中告知。

您是否试图在Spring Boot中读取这些属性?如果是,您可以使用
@ConfigurationProperties(prefix=“yourprix”)