私はスプリングシングルトンビーンで石英スケジューラを開始して停止しようとしています。しかし、ポストコンストラクションは2回呼び出されており、predestroyはまったく呼び出されません。 Thisリンクはプロキシのために2度呼び出されるのが自然だと言っていますが、これはpostconstructメソッドで例外を引き起こしています。私はシングルトンBeanがロードされた後にpostConstructを一度呼び出すだけです。春@PreDestroyと@PostConstructアノテーション
1
A
答えて
0
私はコンテキストローダーリスナーを書き、web.xmlリスナーを変更します。私はBeanを1回だけ初期化できます。
<listener>
<listener-class>
CustomContextLoaderListener
</listener-class>
</listener>
public class CustomContextLoaderListener extends
org.springframework.web.context.ContextLoaderListener{
Scheduler scheduler;
@Override
public void contextInitialized(javax.servlet.ServletContextEvent event) {
try{
super.contextInitialized(event);
this.scheduler= WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(Scheduler.class);
}
@Override
public void contextDestroyed(ServletContextEvent event){
super.contextDestroyed(event);
scheduler.stopSchedulers();
}
0
init-methodを使用して試したり、初期化Beanを実装してみてください。これらはpostConstructに対する代替のアプローチを提供します。
春のBeanがアンロードされたとき、つまりコンテナがシャットダウンされるか、またはConfigurableApplicationContextメソッドのclose()メソッドが他の手段によって呼び出されると、preDestroyが呼び出されます。
関連する問題
- 1. SpringBootApplicationの実行PostConstructとPreDestroy
- 2. PMDは@PostConstructでannotaded方法や@PreDestroy
- 3. 春のオートワイヤリングオーダーと@PostConstruct
- 4. @PostConstruct注釈と春のライフサイクル
- 5. Springブート@PreDestroyアノテーションが動作しない
- 6. 春の挙動Javaメソッドが@Beanと@PostConstruct
- 7. 使用春@Lazyと@PostConstruct注釈
- 8. 春@Component&@Beanアノテーション
- 9. Xpages @PreDestroy
- 10. 春のJavaのToStringとEqualsAndHashCodeアノテーション
- 11. 春@PreDestroy:ログバックが遅すぎるのでログがありません
- 12. 春4 @EnableTransactionManagementアノテーション例外スロー例外
- 13. 春のアノテーション基本的な質問
- 14. RESTEasyのJAX-RSの@PreDestroy
- 15. xmlアノテーションを使用せずに春に@Scheduledアノテーションのスケジューラを追加する
- 16. @postConstruct in JSF 1.1
- 17. java ejb3 @PostConstruct
- 18. @NotNull javaxアノテーションとIntellij @NotNullアノテーション
- 19. @PostConstructとcommandButton/commandLinkのパラメータの場合
- 20. @PostConstructとMessageChannelの自動配線
- 21. @PreDestroyのBeanを呼び出す
- 22. 春ブーツ1.5 @JdbcTest私は春ブーツ1.5.0.RC1に新しい@JdbcTestアノテーションを使用しようとしているユーレカディスカバリー
- 23. 春注釈 - XMLアノテーションを使用したオブジェクト
- 24. 春のセキュリティで@secureアノテーションを追加する方法
- 25. 春 - アノテーション付きのマップでEnumキーを設定する方法
- 26. 春ブーツ - @valueアノテーションを使用することはできません
- 27. @PostConstructの実行順序
- 28. Springツールスイート - org.eclipse.e4.core.di.InjectionException:java.lang.NoClassDefFoundError:javax/annotation/PostConstruct
- 29. @PostConstructメソッドのNULLポインタ例外?
- 30. @ ModelアノテーションとMVC
init-metodも2回呼ばれる – ayengin