我有一个QuartzJob,其中有多个方法调用相同的rest API。
该类包含具有其余api配置的成员(configuration)。
使用getConfiguration方法初始化一次剩余的api配置。
public QuartzJob implements Job{
@Value("${API_URL}")private String apiUrl;
ApiCallConfiguration configuration = getConfiguration();
method1(){call api using configuration }
method2(){call api using configuration }
method3(){call api using configuration }
}
当我尝试创建构造函数时,它失败可能是因为尚未构造API值... @Value(“ $ {API_URL}”)private String apiUrl;
以下例外。
什么都设置好后,在QuartzJob中仅初始化一次配置属性的正确方法是什么?
Caused by: java.lang.NullPointerException: Configuration key Caused by: java.lang.NullPointerException: Configuration key API_URL has no value
at org.apache.commons.lang3.Validate.notBlank(Validate.java:451)
at com.xerox.printerTech.quartz.jobs.PrinterVerificationJob.getPrinterRestApiConfig(PrinterVerificationJob.java:722)
at com.xerox.printerTech.quartz.jobs.PrinterVerificationJob.<init>(PrinterVerificationJob.java:164)
... 10 more has no value
at org.apache.commons.lang3.Validate.notBlank(Validate.java:451)
at com.xerox.printerTech.quartz.jobs.PrinterVerificationJob.getPrinterRestApiConfig(PrinterVerificationJob.java:722)
at com.xerox.printerTech.quartz.jobs.PrinterVerificationJob.<init>(PrinterVerificationJob.java:164)
... 10 more
答案 0 :(得分:0)
我能够使用@Component和@Autowired解决此问题
这是示例代码:
public QuartzJob implements Job{
@Autowired
ApiCallConfiguration configuration;
method1(){configuration.getApiUrl(); }
method2(){configuration.getApiUrl(); }
method3(){configuration.getApiUrl(); }
}
@Component
public class ApiCallConfiguration {
public ApiCallConfiguration(
@Value("${API_URL}") String apiUrl,
@Value("${API_URL_SSL}") String apiUseSSL){
this.apiUrl = apiUrl;
this.apiUseSSL = apiUseSSL;
}
}