java 依赖于其他属性的 Spring 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36136874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:59:25  来源:igfitidea点击:

Spring properties that depend on other properties

javaspring

提问by realsim

I would like to have properties, that I can reference via @Value in spring beans, that can only be created dependend on other properties. In particular I am having a property, that describes the file system location of a directory.

我想要一些属性,我可以在 spring bean 中通过 @Value 引用它,这些属性只能依赖于其他属性创建。特别是我有一个属性,它描述了目录的文件系统位置。

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. 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!). But because of that concatenating Strings on demand is not an option.

现在我想通过我的 bean 中的 @Value 注释同时访问目录和文件。有时我想将它们作为字符串访问,有时作为 java.io.Files 访问它们,有时作为 org.springframework.core.io.FileSystemResource (顺便说一下,它开箱即用!)。但是因为按需连接字符串不是一种选择。

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.Paths.

如您所见,在我的上下文中,我什至创建了一个 PropertyEditor,它可以转换为java.nio.file.Paths。

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. There I get

现在的问题是,出于某种原因,这“在我的机器上运行”(在我的 IDE 中),但不能在预期的目标环境中运行。我得到了

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

回答by Sylvain Deschenes

Spring can combine properties

Spring可以组合属性

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

You can also use a default value without defining your myFilein the properties at first:

您还可以使用默认值而不首先myFile在属性中定义您的:

Properties file

属性文件

myDir=/path/to/mydir

In class:

在班上:

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