私はSpringブート自動設定@EnableScheduling
を使ってcronジョブを作成しようとしています。 fixedRateString
がハードコーディングされている場合、ジョブは完全に正常に起動します。しかし、私がSpELを使って価値を提供しようとすると成功しません。SpELがインスタンス変数にアクセスできない
//ビーンインスタンスJavaConfigで
@Bean
public AlertbotJob2 getAlertJob2() {
AlertbotJob2 alertbotJob2 = new AlertbotJob2("alertId", "alertName", "alertSubject", "6000", true);
return alertbotJob2;
}
Bean定義
public class AlertbotJob2 {
String alertId;
String alertName;
String alertSubject;
public String cronPattern;
boolean isActive;
public AlertbotJob2() {
}
public AlertbotJob2(String alertId, String alertName, String alertSubject, String cronPattern, boolean isActive) {
super();
this.alertId = alertId;
this.alertName = alertName;
this.alertSubject = alertSubject;
this.cronPattern = cronPattern;
this.isActive = isActive;
}
@Scheduled(initialDelay = 60000, fixedRateString = "#{this.cronPattern}")
public void doTheJob() {
System.out.println("DoSomething");
}
}
例外は::式の解析に失敗しました。ネストされた例外はorg.springframework.expression.spel.SpelEvaluationExceptionです:EL1008E: 'org.springframework.beans.factory.config.BeanExpressionContext'型のオブジェクトでプロパティまたはフィールド 'this'を見つけることができません。
私はトリガー値を動的に設定しようとしています。助けてください。
私はそのアプローチを試みましたが、うまく機能しませんでした。データベース構成を読み込んだ後、プログラムで仲間と一緒にBeanを作成しています。 プロトタイプスコープで**@someBean**.cronPatternを作成できるかどうかを調べる必要があります。 – zikzakjack
いいえ; SpEL式が注釈が存在するクラスのインスタンスを直接参照する方法はありません。 –