Tanu Garg
Tanu Garg

Reputation: 3067

How to use application.properties in org.quartz.Job Class

I have created a spring boot application to implement Quartz scheduler. In Job class, I want to use some property from application.properties. How to inject that?

I have tried below but getting null:

    @Component
public class ScheduleJob implements org.quartz.Job {

        private static final Logger LOGGER = LogManager.getLogger(ScheduleJob.class);

        public ScheduleJob() {
        }

        @Value("${ijobs.service.url}")
        private String ijobsUrl;

        public void execute(JobExecutionContext context) throws JobExecutionException {

            LOGGER.info("Job exceution starts--->");
              System.out.println("-------------------"+ijobsUrl);

Upvotes: 1

Views: 1862

Answers (2)

Mufaddal Tahir
Mufaddal Tahir

Reputation: 539

Firstly, the ScheduleJob class should be annotated with @Component for spring to manage it. Secondly, if you want any property to be injected you could do it in a similar way to how you are injecting value of ijobsUrl.

put your required property in application.properties

@Value("${my.property}")
private String myProperty

Upvotes: 0

Daniel Sagenschneider
Daniel Sagenschneider

Reputation: 73

Spring requires annotating the class with @Component for it to manage it (including loading any properties into the class). If you use "new" to create the object, then it is outside Spring's control and it won't inject the dependencies/values into it.

On side note, there is native support for Quartz if using Spring Boot: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html

Upvotes: 1

Related Questions