Forums: Open Discussion (Thread #19858)

リスナークラスからのDB更新について (2008-09-08 16:58 by Anonymous #38747)

現在Server Framework for Java WEB版を使用して下記の処理を作成しております。。。

【処理】
HttpSessionAttributeListener等でセッションリスナークラスを実装し、あるセッションが破棄された時にDBに対して更新を行う処理を作成しています。

【疑問】
この時のDBに対して更新を行う処理の実装方法なのですが、applicationContext.xml等を使用して容易にDB更新を行う方法というのは可能でしょうか?
セッションリスナー実装クラスにオブジェクトをインジェクトすることが可能なのでしょうか。。

何かいい方法をご教授いただければ、幸いです。
以上、宜しくお願いいたしま

Reply to #38747×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: リスナークラスからのDB更新について (2008-09-08 19:51 by kimumasa #38755)


疑問がListenerにDIしたいのか、DB更新したいだけなのかよく分りませんが。。。

とりあえず一時回答をすると
ご希望と思われる動きを実現するにはHttpSessionListener内で
DIコンテナから管理しているDAOを取得する事になると思います。
ご参考までにサンプルコードとしては以下のような感じです。

「あるセッション」という記述がさらに謎な感じですが
特定のsessionIdに対してのみListenerを走らせると言う事は無理ですので
HttpSessionEvent.getSession()をコールして条件分岐させてもらえればと
思います。

====

package jp.terasoluna.sample;

import java.util.Map;

import javax.servlet.http.HttpSessionEvent;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.web.HttpSessionListenerImpl;

/**
* 蛇足ですがサンプルコードでは一時セッションディレクトリ機能を
* 利用している前提でHttpSessionListenerImplを継承しています。
* 不要であれば、随時読み替えてください。
**/
public class SampleSessionListener extends HttpSessionListenerImpl {

@Override
public void sessionDestroyed(HttpSessionEvent event) {
super.sessionDestroyed(event);
WebApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(event.getSession().getServletContext());
// 例えばQueryDAOを取得する場合
// QueryDAO queryDAO = (QueryDAO) ctx.getBean("queryDAO");
// Map<String, Object> map = queryDAO.executeForMap("test", null);
}
}

====
Reply to #38747

Reply to #38755×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: リスナークラスからのDB更新について (2008-09-08 19:58 by kimumasa #38756)

HttpSessionListenerではなく
HttpSessionAttributeListenerでしたか・・

とはいえ、回答としてはあまり変わらないと思いますので
ご参考まで。
Reply to #38755

Reply to #38756×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login