Spring: how to set @DateTimeFormat template from properties file?

I ask the same question that I asked here because the answers provided in it do not solve my problem.

I am using Spring 4.1.3 in a Spring MVC web application. I have a JPA bean object containing a field:

@DateTimeFormat(pattern = "${date.format}")
private LocalDate certifiedOn;

I would like the template to be entered based on the key in the properties file. This should work in Spring starting with version 3.0.3 , however the binding does not work in the controller since Pattern includes reserved character '{'.

I know that I am successfully reading my properties file, as the application uses other properties. Here is how I set this setting through javaConfig:

@Configuration
@ComponentScan(basePackages = "com")
@PropertySource("classpath:spring.properties")
public class AppConfig {

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
  ...
}

The root directory of the path contains the file spring.properties:

date.format = yyyy-MM-dd

What am I missing here so that this does not help?

+4
1

, , , . :

application.properties

date.format=yyyy-MM-dd

Spring -

public class MyClass {
private static final Logger log = LoggerFactory.getLogger(MyClass.class);

@DateTimeFormat(pattern = "${yyyy-MM-dd}")
private LocalDate certifiedOn = LocalDate.now();

@Override
public void afterPropertiesSet() throws Exception {
    log.info("Date format used: " + certifiedOn);
}
}

: 2015-01-22

LocalDate.now(), .

certifiedOn, :

: null

null - , certifiedOn, , date.format , .

, , . , , , , ?

0

Source: https://habr.com/ru/post/1568075/


All Articles