简体   繁体   English

依赖于其他属性的 Spring 属性

[英]Spring properties that depend on other properties

I would like to have properties, that I can reference via @Value in spring beans, that can only be created dependend on other properties.我想要一些属性,我可以在 spring bean 中通过 @Value 引用它,这些属性只能依赖于其他属性创建。 In particular I am having a property, that describes the file system location of a directory.特别是我有一个属性,它描述了目录的文件系统位置。

myDir=/path/to/mydir

And by convention, there is a file in that directory, that is always called myfile.txt .按照惯例,该目录中有一个文件,始终称为myfile.txt

Now i want to have access to both, the directory and the file, via @Value annotations inside my beans.现在我想通过我的 bean 中的 @Value 注释同时访问目录和文件。 And sometimes I want to access them as Strings, sometimes as java.io.Files and sometimes as org.springframework.core.io.FileSystemResource (which by the way works very well out of the box!).有时我想以字符串的形式访问它们,有时以 java.io.Files 的形式访问它们,有时以 org.springframework.core.io.FileSystemResource 的形式访问它们(顺便说一句,它开箱即用!)。 But because of that concatenating Strings on demand is not an option.但是因为按需连接字符串不是一种选择。

So what I of course could do is just declare both, but I would end up with所以我当然可以做的只是声明两者,但我最终会

myDir=/path/to/mydir
myFile/path/to/mydir/myfile.txt

and I would like to avoid that.我想避免这种情况。

So I came up with an @Configuration class, that takes the property and adds it as new PropertySource:所以我想出了一个@Configuration 类,它获取属性并将其添加为新的 PropertySource:

@Autowired
private ConfigurableEnvironment environment;

@Value("${myDir}")
private void addCompleteFilenameAsProperty(Path myDir) {
    Path absoluteFilePath = myDir.resolve("myfile.txt");

    Map<String, Object> props = new HashMap<>();
    props.put("myFile, absoluteFilePath.toString());
    environment.getPropertySources().addFirst(new MapPropertySource("additional", props));
}

As you can see, in my context I even created a PropertyEditor, that can convert to java.nio.file.Path s.如您所见,在我的上下文中,我什至创建了一个 PropertyEditor,它可以转换为java.nio.file.Path s。

Now the problem is, that for some reason, this "works on my machine" (in my IDE), but does not run on the intended target environment.现在的问题是,出于某种原因,这“在我的机器上工作”(在我的 IDE 中),但不能在预期的目标环境上运行。 There I get我得到了

java.lang.IllegalArgumentException: Could not resolve placeholder 'myFile' in string value "${myFile}"

Spring can combine properties Spring可以结合属性

myDir=/path/to/mydir 
myFile=${myDir}/myfile.txt

You can also use a default value without defining your myFile in the properties at first: 您也可以使用默认值,而不首先在属性中定义myFile

Properties file 属性文件

myDir=/path/to/mydir

In class: 在班上:

@Value("#{myFile:${myDir}/myfile.txt}")
private String myFileName;

Spring expressions can be used to refer the properties. Spring 表达式可用于引用属性。

In my example it was在我的例子中是

query-parm=QueryParam1=
query-value=MyParamaterValue

Now while binding them in Spring Bean.现在在 Spring Bean 中绑定它们。

 @Configuration
    public class MyConfig {
    @Value("${query-param}${query-value}")
    private String queryString;
 }

Above code will inject QueryParam1=MyParamaterValue to the variable queryString.上面的代码将QueryParam1=MyParamaterValue注入变量 queryString。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM