복붙노트

[SPRING] 속성 파일에서 Spring @ Scheduled cron 세부 정보 - 예외

SPRING

속성 파일에서 Spring @ Scheduled cron 세부 정보 - 예외

내 봄에 @ cceduled 메서드에서 cron 세부 정보를 정의하려고했습니다.

@Service
@PropertySource("classpath:application.properties")
public class CacheRefreshService {

@Scheduled(cron = "${api.refresh.cron}")
     public void refreshJob() throws Exception {
        LOGGER.info("Started Refresh");
        //do something
     }
}

그리고 내 application.properties

#Refresh
api.refresh.cron =0 29 11 * * ?

@Scheduled와 함께 cron 세부 정보를 정의하면 잘 돌아갑니다. 하지만 이렇게하면 속성 파일에서 값을 읽을 수 없으며 아래 오류가 발생합니다.

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'refreshJob': Cron expression must consist of 6 fields (found 1 in "${api.refresh.cron}")

어떤 제안이라도 제발?

해결법

  1. ==============================

    1.내 ApplicationContext에 아래 내용을 추가하면 문제가 해결되었습니다.

    내 ApplicationContext에 아래 내용을 추가하면 문제가 해결되었습니다.

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

    2.factoryBean.setCronExpression ( "0 0/1 * 1/1 *? *"); 당신은 Cron Expresssion bcz를 BeanFactory 클래스에 설정해야합니다. setCronExpression은 상속됩니다

    factoryBean.setCronExpression ( "0 0/1 * 1/1 *? *"); 당신은 Cron Expresssion bcz를 BeanFactory 클래스에 설정해야합니다. setCronExpression은 상속됩니다

  3. from https://stackoverflow.com/questions/34195290/spring-scheduled-cron-details-from-property-file-exception by cc-by-sa and MIT license