10

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)?

3 Answers 3

8

To add to jst's answer, 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);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

I put this into a @Configuration class and made the Converter<String, Path> a bean (@Bean). That took care of registration using Spring Boot
2

I took up james idea and defined the converter within the spring boot configuration:

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

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

    @Bean
    @ConfigurationPropertiesBinding
    public PathConverter getStringToPathConverter() {
        return new PathConverter();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.