Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject java.nio.file.Path dependency using @ConfigurationProperties

I'm using Spring Boot and have the following Component class:

@Component
@ConfigurationProperties(prefix="file")
public class FileManager {

    private Path localDirectory;

    public void setLocalDirectory(File localDirectory) {
        this.localDirectory = localDirectory.toPath();
    }

...

}

And the following yaml properties file:

file:
     localDirectory: /var/data/test

I would like to remove the reference of java.io.File (of setLocalDirectory) by replacing with java.nio.file.Path. However, I receive a binding error when I do this. Is there way to bind the property to a Path (e.g. by using annotations)?

like image 397
James Avatar asked Jun 25 '15 18:06

James


People also ask

What is @ConfigurationProperties annotation in spring boot?

@ConfigurationProperties allows to map the entire Properties and Yaml files into an object easily. It also allows to validate properties with JSR-303 bean validation. By default, the annotation reads from the application. properties file. The source file can be changed with @PropertySource annotation.

What is the use of @ConfigurationProperties in spring boot?

@ConfigurationProperties works best with hierarchical properties that all have the same prefix; therefore, we add a prefix of mail. The Spring framework uses standard Java bean setters, so we must declare setters for each of the properties. That's it!

How do you specify file path in application properties in spring boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

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.


2 Answers

To add to jst's answer above, the Spring Boot annotation @ConfigurationPropertiesBinding can be used for Spring Boot to recognize the converter for property binding, as mentioned in the documentation under Properties Conversion:

@Component
@ConfigurationPropertiesBinding
public class StringToPathConverter implements Converter<String, Path> {

  @Override
  public Path convert(String pathAsString) {
    return Paths.get(pathAsString);
  }
}
like image 91
Sebastian Avatar answered Oct 02 '22 19:10

Sebastian


I don't know if there is a way with annotations, but you could add a Converter to your app. Marking it as a @Component with @ComponentScan enabled works, but you may have to play around with getting it properly registered with the ConversionService otherwise.

@Component
public class PathConverter implements Converter<String,Path>{

 @Override
 public Path convert(String path) {
     return Paths.get(path);
 }

When Spring sees you want a Path but it has a String (from your application.properties), it will lookup in its registry and find it knows how to do it.

like image 35
jst Avatar answered Oct 02 '22 18:10

jst