Load localized messages in annotations - @CsvBindByName

15 views Asked by At

I am trying to load localized messages from messages.properties in Resource Bundle into the @CsvBindByName annotation in opencsv.

My goal here is to be able to load or change values from the messages.properties file at anytime without changing it in the @CsvBindByName annotation.

This is my MessageConfig:

@Bean("messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("classpath:messages");
    messageSource.setDefaultEncoding("UTF-8");

    return messageSource;
}

@Bean
public LocalValidatorFactoryBean getValidator() {
    LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
    bean.setValidationMessageSource(messageSource());
    return bean;
}

I have the following properties defined in messages.properties:

global.report.customer.nbr.csv.column=Customer Number

test.error.code.mandatory=Code is mandatory
test.error.code.size=Code size must be between 1 and 5

I have defined the following variable in the object I want to write to my CSV file as follows:

@CsvBindByPosition(position = 1)
@CsvBindByName(column = "{global.report.customer.nbr.csv.column}")
private String customerNbr;

It outputs {global.report.customer.nbr.csv.column} in my CSV file instead of the message defined in my Resource Bundle.

However, if I do the same with the following annotations from jakarta.validation.constraints:

@NotNull(message = "{test.error.code.mandatory}")
@Size(min = 1, max = 5, message = "{test.error.code.size}")

This works fine by outputting the messages in my Resource bundle.

How can I inject values from Resource bundle into annotations such as @CsvBindByName?

0

There are 0 answers