Struts2中整合了Spring,配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
同理,在自己的代码里边希望系统启动的时候执行自定义的个一个任务,所以自己又新定义了一个listener,进行轮巡数据库的操作,在listener-class的类中,定义了Timer和TimerTask,TimerTask中的动作想依赖Spring的注入,不过发现,只是在applicationContext.xml中定义TimerTask和它的property,property是不能被加载的。
大概代码为:
applicationContext.xml如下:
<bean id="task" class="com.ll.listener.Task">
<property name="service" ref ="service" />
</bean>
web.xml如下:
<listener>
<listener-class>com.ll.listener.Listener</listener-class>
</listener>
listener.java如下:
private Timer timer = new Timer();
private Task task = new Task();
...
timer.schedule(task, delay, period);
当进入Task的run()方法之后,会使用注入到task里边的service,不过这时候发现service为空,说明注入没起作用,不知道在struts里边该如何处理这样的问题!