如何在类中获取spring上下文 急!!

wangpeng88888888 2008-11-30 12:41:41
我现在用的是Spring+hibernate+struts框架 用的是spring的代理模式.
现在我做了一个session失效后的方法public class SessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent event)
{
HttpSession session = event.getSession();

System.out.println(session.getId());
}

public void sessionDestroyed(HttpSessionEvent event)
{
HttpSession session = event.getSession();
String userID = (String) session.getAttribute("userId");
//在这里获取spring上下文.
}

请问该怎么获取spring上下文
<bean id="loginTempService" parent="baseTxService">
<property name="target" ref="loginTempServiceTarget"/>
</bean>
<bean name="loginTempServiceTarget" class="com.panasonic.fsdc.service.impl.LoginTempService" >
<property name="loginTempDao">
<ref bean="loginTempDao"/>
</property>
</bean>

这是配置文件
...全文
2927 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
East271536394 2008-12-03
  • 打赏
  • 举报
回复
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
前提要求是applicationContext.xml在src目录下面
FileSystemXmlApplicationContext appContext=new ClassPathXmlApplicationContext("f:applicationContext.xml的路径");
这里是在任何目录下都能找到的
Jerry0006 2008-12-02
  • 打赏
  • 举报
回复
以下几种是经常用到的:
1、FileSystemXmlApplicationContext——从指定的目录中加载:
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

2、ClassPathXmlApplicationContext——从classpath路径加载:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

不同的是,第一种只能从指定的目录中加载,而第二种可以从整个classpath路径中加载。

至于从WEB容器中的加载方式,则可以从web.xml中配置加载。
hrhsdz 2008-12-01
  • 打赏
  • 举报
回复
ServletContext context = arg0.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
yourService = (YourService)ctx.getBean("yourService");
zou_wei_forever 2008-12-01
  • 打赏
  • 举报
回复

可以实现BeanFactoryAware接口,实现BeanFactoryAware接口的Bean类,在依赖关系设定完成后,初始化方法之前,Spring容器将会注入BeanFactory的实例,你可以通过这个BeanFactory的实例获取其他的bean,不过这样你的bean就和spring耦合到一块了。
wanchangjun2005 2008-11-30
  • 打赏
  • 举报
回复
然后appContext.getBean("loginTempService");
wanchangjun2005 2008-11-30
  • 打赏
  • 举报
回复
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
前提要求是applicationContext.xml在src目录下面
FileSystemXmlApplicationContext appContext=new ClassPathXmlApplicationContext("f:applicationContext.xml的路径");
这里是在任何目录下都能找到的
然月枕流君 2008-11-30
  • 打赏
  • 举报
回复
实话...没看明白什么意思...
你要获得什么样的上下文...

获取上下文:
session.getServletContext();

或者是sping的上下文:
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = (BeanFactory) appContext;
factory.getBean("loginTempService");

Landor2004 2008-11-30
  • 打赏
  • 举报
回复
	@Override
public void sessionDestroyed(HttpSessionEvent event) {

ServletContext context = event.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
LoginTempService loginTempService = (loginTempService)ctx.getBean("loginTempService");
}
Landor2004 2008-11-30
  • 打赏
  • 举报
回复
ServletContext context = arg0.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
yourService = (YourService)ctx.getBean("yourService");

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧