私は春のアプリケーションを持っています。私はクラスをautowiringしていて、うまく動作しています。例えばのために 春はサーバーの起動時にクラスをオートワイヤーします
@Controller
public class SearchController {
@Autowired
private EnvironmentControl envControl;
@Autowired
private SearchControl searchControl;
...
しかし、今、私は何かをスケジュールするためにinitメソッドを使用していますScheduleServletと呼ばれるサーバーの起動クラスに持っている...これで
public class SchedulerServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.LOGGER.info("timer servlet is initialized ");
try {
InitialContext ic = new InitialContext();
TimerManager tm = (TimerManager) ic.lookup("java:comp/env/tm/TimerManager");
Timer timer = tm.schedule(new GlobalTemplateScheduler(), 0, 3600000);// one hour interval
System.out.println("Timer..... " + timer);
}
...
私GlobalTemplateSchedulerクラスが実行されるようにスケジュールされtimerExpired方法があります1時間間隔ごとに
public class GlobalTemplateScheduler implements TimerListener {
@Autowired
private TemplateControl templateControl;
@Override
public void timerExpired(Timer timer) {
try {
templateControl.updateMappings(names);
} catch (Exception e) {
this.LOGGER.error(e.getMessage());
e.printStackTrace();
}
...
私はnullを取得しているtemplateControlをautowireする必要があります。これはサーバーの起動時に発生します。
さらに内部updateMappingsもコンストラクタ、引数としてautowiredされるデータソースオブジェクトは、(これは、ブラウザのリクエストに応じて細かい作業が、サーバの起動時にそれを実行する必要があります)があります。
注:私はApplicationListenerインタフェースを使用することはできません。
どのような提案が本当に役に立ちます。
ありがとうございます。アプリケーションの起動豆の初期化で
あなたは、newを呼び出すことによってあなたのGlobalTemplateSchedulerをインスタンス化します。これはあなたのコントロール下にあることを意味し、Springのものではありません。既に発見したように、autowiring注釈はそのような場合には何の効果もありません。 GlobalTemplateSchedulerとSchedulerServlet Spring Beanを作成し、注釈を付ける必要があります。ただ注意してください:このアプリケーションをクラスタで実行する場合は、インスタンスごとに1つの「グローバル」スケジューラが必要です。 – duffymo