1

Is there a way to pass a variable from a property file to a class annotation?

@MaxPeriod(firstDateField = "startDateFrom", secondDateField = "startDateTo", maxPeriod = "${variable.from.property:31}")
public class ProcessReportFilter {
    private LocalDate startDateFrom;
    private LocalDate startDateTo;
}

upd: annotation is used in the request object at bean validation

@Target({ElementType.TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = MaxPeriodValidator.class)
@Repeatable(MaxPeriod.List.class)
public @interface MaxPeriod {

  String message() default "Period between {firstDateField} and {secondDateField} must not exceed {maxPeriod} day(-s)";

  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};

  String firstDateField();

  String secondDateField();

  long maxPeriod();

  @Target({ElementType.TYPE})
  @Retention(RUNTIME)
  @interface List {
    MaxPeriod[] value();
  }
}
  • Does this answer your question? [How can I inject a property value into a Spring Bean which was configured using annotations?](https://stackoverflow.com/questions/317687/how-can-i-inject-a-property-value-into-a-spring-bean-which-was-configured-using) – Vadim Beskrovnov Dec 01 '22 at 17:01
  • Are you adding **variable.from.property** in .properties? So this should work like your example. – Dilermando Lima Dec 01 '22 at 17:09
  • @VadimBeskrovnov, Thanks. In the post on the link, we talk about beans, and I have a request object. It's about bean validation – ALEKSEY EMETS Dec 02 '22 at 06:04
  • @DilermandoLima, in my case variable looked as string at validator code – ALEKSEY EMETS Dec 02 '22 at 06:05
  • Hmm, trying to add the validator code, but getting the error "It looks like your post is mostly code; please add some more details." – ALEKSEY EMETS Dec 02 '22 at 06:07
  • @ALEKSEYEMETS then add some more details :) describe why it does not work, what your expected result is vs the actual result, etc. – knittl Dec 02 '22 at 07:27

0 Answers0