How can i check if "${path}" value is not empty or is correct path and if so, throw some exception? I want to it happen during Bean creation. All I found that is such validation used in multilayer apps for user input data validation in @Controller classes with using @Valid annotation. But what i did was not working.
It a simple Spring app, that reading application.properties and somehow processes them. Here is my code:
@Component
public class ParseService {
@Value("${path}")
@PathValue
private String path;
}
@Documented
@Constraint(validatedBy = PathValidatorImpl.class)
@Retention(RUNTIME)
@Target({ElementType.FIELD})
public @interface PathValue {
String message() default "Is empty or not valid path!";
}
public class PathValidatorImpl implements ConstraintValidator<PathValue,
String> {
@Override
public void initialize(PathValue pathValue) {
}
@Override
public boolean isValid(String path, ConstraintValidatorContext ctx) {
if (path.isEmpty() || !(new File(path).exists())) {
return false;
} else
return true;
}
}
Can I do this and if so, What am I doing wrong?
I tried this:
@Component
public class FileGuider {
public List<File> search(@Valid String path, String sourceFileExtension)
throws IOException, NoFilesToParseException {...}
P.S. I use Spring in this app for studying.