以下のコードリスナーがあなたのweb.xml
で以下の行を追加します。追加するには、
public class MyListener implements ServletContextListener {
private ApplicationContext applicationContext;
private MyDAO myDAO;
public void contextInitialized(ServletContextEvent event) {
applicationContext = getContext(event);
myDAO = applicationContext.getBean("myDAO");
performAction();
}
public void contextDestroyed(ServletContextEvent event) {
}
/**
* Gets the ApplicationContext from the ServletContextEvent.
*
* @param event
* @return ApplicationContext.
*/
private ApplicationContext getContext(ServletContextEvent event) {
return WebApplicationContextUtils
.getRequiredWebApplicationContext(event.getServletContext());
}
void performAction(){
myDAO.getTheNeededData();
}
}
をお手伝いをします、
<listener>
<listener-class>com.foo.MyListener</listener-class>
</listener>
それは誤り与える:java.lang.IllegalStateExceptionを:いいえWebApplicationContextが見つかりました: ContextLoaderListenerは登録されていませんか? –
リスナーを登録する方法を示す回答を更新しました。 – ManuPK
私はすでにlistner ....と同じエラーを登録しています。 –