Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring properties that depend on other properties

Tags:

java

spring

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.

myDir=/path/to/mydir 

And by convention, there is a file in that directory, that is always called 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.

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:

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

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

java.lang.IllegalArgumentException: Could not resolve placeholder 'myFile' in string value "${myFile}" 
like image 850
realsim Avatar asked Mar 21 '16 16:03

realsim


People also ask

Which property is given precedence by spring?

Spring Boot application converts the command line properties into Spring Boot Environment properties. Command line properties take precedence over the other property sources. By default, Spring Boot uses the 8080 port number to start the Tomcat.

What 2 types of file formats can use to inject properties into the spring environment object?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.

How do I apply different application properties in spring boot?

Simply put application. properties file is a configuration file in which we put configuration key-value pairs for our spring boot application. Spring boot uses these configurations during startup to configure various properties like port no, context path, database configuration, and many other configurations.

Can we use both application properties and application yml in spring boot?

Overview. A common practice in Spring Boot is using an external configuration to define our properties. This allows us to use the same application code in different environments. We can use properties files, YAML files, environment variables and command-line arguments.


1 Answers

Spring can combine properties

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

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

Properties file

myDir=/path/to/mydir 

In class:

@Value("#{myFile:${myDir}/myfile.txt}") private String myFileName; 
like image 51
Sylvain Deschenes Avatar answered Sep 20 '22 19:09

Sylvain Deschenes