通常のサーブレットの場合、 context listenerを宣言できると思いますが、Spring MVC の場合、Spring はこれを簡単にしますか?
さらに、コンテキスト リスナーを定義してから、servlet.xml
またはで定義された Bean にアクセスする必要がある場合applicationContext.xml
、どのようにそれらにアクセスできますか?
通常のサーブレットの場合、 context listenerを宣言できると思いますが、Spring MVC の場合、Spring はこれを簡単にしますか?
さらに、コンテキスト リスナーを定義してから、servlet.xml
またはで定義された Bean にアクセスする必要がある場合applicationContext.xml
、どのようにそれらにアクセスできますか?
Spring には、処理できる標準イベントがいくつかあります。
ApplicationListener
これを行うには、次のようなインターフェイスを実装する Bean を作成して登録する必要があります。
package test.pack.age;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class ApplicationListenerBean implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
// now you can do applicationContext.getBean(...)
// ...
}
}
}
次に、この Bean をservlet.xml
orapplicationContext.xml
ファイルに登録します。
<bean id="eventListenerBean" class="test.pack.age.ApplicationListenerBean" />
Spring は、アプリケーション コンテキストが初期化されると通知します。
Spring 3 (このバージョンを使用している場合) では、ApplicationListener
クラスはジェネリックであり、関心のあるイベント タイプを宣言でき、それに応じてイベントがフィルター処理されます。次のように、Bean コードを少し単純化できます。
public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
// now you can do applicationContext.getBean(...)
// ...
}
}
Spring 4.2以降、使用できます@EventListener
(ドキュメント)
@Component
class MyClassWithEventListeners {
@EventListener({ContextRefreshedEvent.class})
void contextRefreshedEvent() {
System.out.println("a context refreshed event happened");
}
}
複数のデータベースからのデータを含むHashMap(私のWebページで使用される)を作成するURLを入力する単一ページアプリケーションがありました。サーバーの起動時にすべてをロードするために、次のことを行いました-
1 - 作成された ContextListenerClass
public class MyAppContextListener implements ServletContextListener
@Autowired
private MyDataProviderBean myDataProviderBean;
public MyDataProviderBean getMyDataProviderBean() {
return MyDataProviderBean;
}
public void setMyDataProviderBean(MyDataProviderBean MyDataProviderBean) {
this.myDataProviderBean = MyDataProviderBean;
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent context) {
System.out.println("ServletContextListener started");
ServletContext sc = context.getServletContext();
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(sc);
MyDataProviderBean MyDataProviderBean = (MyDataProviderBean)springContext.getBean("myDataProviderBean");
Map<String, Object> myDataMap = MyDataProviderBean.getDataMap();
sc.setAttribute("myMap", myDataMap);
}
2- web.xml に以下のエントリを追加
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.context.listener.MyAppContextListener</listener-class>
</listener>
3-コントローラークラスで、最初にservletContextのMapをチェックするようにコードを更新しました
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) {
Map<String, Object> myDataMap = new HashMap<String, Object>();
if (context != null && context.getAttribute("myMap")!=null)
{
myDataMap=(Map<String, Object>)context.getAttribute("myMap");
}
else
{
myDataMap = myDataProviderBean.getDataMap();
}
for (String key : myDataMap.keySet())
{
model.addAttribute(key, myDataMap.get(key));
}
return "myWebPage";
}
Tomcat を起動すると、この多くの変更により、startTime 中に dataMap がロードされ、すべてが servletContext に配置されます。これは、コントローラー クラスによって使用され、既に設定されている servletContext から結果を取得します。