Listener中怎么注入service

KG071 2011-05-14 11:17:00
在web.xml中配置了MyListener,MyListener中有个myService,有个setMyService方法,在xml文件中配置了


<bean id="myListener" class="com.MyListener" scope="prototype">
<property name="myService" ref="myService"/>
</bean>


但是MyListener中的那个myService一直都是null,我该怎么注入?
...全文
463 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
KG071 2011-05-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 burningbloog 的回复:]

那你的myService已经配置了吗?一般注入的都是普通的bean啊,如果是Listener就不一定对了。因为Listener在容器启动时会初始化,而容器启动时也会读取applicationcontext.xml进行bean的装配。
[/Quote]

是Listener 已经用getBean方法拿到了service
Inhibitory 2011-05-15
  • 打赏
  • 举报
回复
你这个是Spring吧,Spring的配置内容要放在Spring的配置文件里,如app-servlet.xml里,不是放在web.xml中
burningbloog 2011-05-15
  • 打赏
  • 举报
回复
那你的myService已经配置了吗?一般注入的都是普通的bean啊,如果是Listener就不一定对了。因为Listener在容器启动时会初始化,而容器启动时也会读取applicationcontext.xml进行bean的装配。
KG071 2011-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yingzhuo2011 的回复:]

Java code

ServletContext sc = request.getSession(true).getServletContext();
ApplicationContext beans = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
MyService service = (MyServic……
[/Quote]

你的做法跟2楼的做法差不多,拿到那个service了。

问个问题,一般项目中自己写的Listener会很多吗?
yingzhuo2011 2011-05-15
  • 打赏
  • 举报
回复

ServletContext sc = request.getSession(true).getServletContext();
ApplicationContext beans = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
MyService service = (MyService) beans.getBean("myService");


以上代码就可以啦。 你在监听器里可以用service了。 可能不满足你的要求,这毕竟不是注入。

我不知道是否可以注入,我一般都是这么干的。
yingzhuo2011 2011-05-15
  • 打赏
  • 举报
回复
嗯。 我也不知道Linstener能不能放到Ioc容器里面去。 如果能放进去的话还是可以注入的。
wl_ldy 2011-05-14
  • 打赏
  • 举报
回复

参考:http://topic.csdn.net/u/20090608/17/669e10c2-30c7-47e9-baed-2bf3dfc8cb26.html
KG071 2011-05-14
  • 打赏
  • 举报
回复
在做毕业设计 所以挺急的

67,513

社区成员

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

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