2

My Properties class:

@Data
@Configuration
@ConfigurationProperties(prefix = "application.keys")
public class MyProperties {

    private Duration duration;
    private String cronValue;

}

In application.yml file:

application:
  keys:
    duration: PT1H
    cron-value: "* 0/1 * * * *"

My Scheduler class:

@Service
@RequiredArgsConstructor
public class MyScheduler {

    private final MyProperties myProperties;

    @Scheduled(cron = "#{myProperties.cronValue}")
    public void run() {
        System.out.println(OffsetDateTime.now().plus(myProperties.getDuration()));
    }

}

It is not working as well:

@Scheduled(cron = "${application.keys.cron-value}")

“Could not resolve placeholder application.cron-value”

  • Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:617 – Developer1234567 Jul 28 '21 at 06:49
  • Caused by: java.lang.IllegalStateException at ScheduledAnnotationBeanPostProcessor.java:511 – Developer1234567 Jul 28 '21 at 06:49
  • 1
    Don't add additional code as comments that is unreadable. Ofcourse `${application.cron-value}` will fail as the property is named `application.keys.cron-value`. – M. Deinum Jul 28 '21 at 07:06
  • @M.Deinum Thank you for your reply. Sorry, corrected. It is example and all keys were correct. It is still not working. – Developer1234567 Jul 28 '21 at 07:18
  • @M.Deinum `@Scheduled(cron = "#{myProperties.keys.cronValue}")` not working according to example and with `@Value` as well – Developer1234567 Jul 28 '21 at 07:20
  • This will only work with Spring Boot and if you correctly use Spring Boot to bootstrap your application. If you try this in a standard spring application it won't work (additional work is required to load the yaml file then). How things are loaded isn't really clear from your question. – M. Deinum Jul 28 '21 at 07:20
  • Again the name of the property is `application.keys.cron-value`. If you want to use a value expression use that `@Value("application.keys.cron-value").` If you want to reference the bean property it is named `myProperties.cronValue` **not** `myProperties.keys.cronValue`. So if you want to use the bean expression use `@Value("#{@myProperties.cronValue})'. With the `@` you instruct SpEl to lookup the bean. – M. Deinum Jul 28 '21 at 07:21
  • @M.Deinum I use gradle `plugins { id 'java' id 'org.springframework.boot' version '2.4.1' }` – Developer1234567 Jul 28 '21 at 07:30
  • @M.Deinum it is not working. Very strange – Developer1234567 Jul 28 '21 at 08:21

1 Answers1

0

In application.yml file:

application:
  keys:
    duration: PT1H
    cron-value: "* 0/1 * * * *"

Create the AppProperties

@Configuration
@ConfigurationProperties(prefix = "application")
@EnableConfigurationProperties
public class AppProperties {

    private Map<String, String> keys = new HashMap<>();

    public Map<String, String> getKeys() {
        return keys;
    }

    public void setKeys(Map<String, String> keys) {
        this.keys = keys;
    }
}

Then Create Appconstants

public class AppConstant {

    public static final String DURATION = "duration";

    public static final String CRONVALUE = "cron-value";

}

Test class

@Autowired
    private AppProperties appProps;
public void getMsg() {
            Map<String, String> msgs = appProps.getKeys();
            System.out.println(msgs.get(AppConstant.DURATION));
            System.out.println(msgs.get(AppConstant.CRONVALUE));
        }

it will print the value from the application properties