日期:2014-05-17  浏览次数:20666 次

系统启动时的spring注入……
我在系统启动时,想通过ServletContextListener加载数据库的常用数据,以下是部分代码
public class initSystemListener implements ServletContextListener
{
private ServletContext context = null;
public void contextDestroyed(ServletContextEvent arg0)
{
// TODO Auto-generated method stub

}
public void contextInitialized(ServletContextEvent arg0)
{
context = arg0.getServletContext();
Configer.getInstance("systemConfig").loadSysConfig();
Configer.virtualRoot = context.getInitParameter("virtualRoot");
}
}
这里Configer是一个普通bean,我想在这个bean中注入一个service,见下
public class Configer
{
public static String virtualRoot;
private ICommon sysConfigService;
public void setSysConfigService(ICommon sysConfigService)
{
this.sysConfigService = sysConfigService;
}
我也作了相应配置
<bean id="configer" class="com.family168.init.Configer" >
<property name="sysConfigService">
  <ref bean="sysConfigService" />
</property>
</bean>
可是通过断点跟踪发现,根本没有执行setSysConfigService方法,sysConfigService 一直为null,我这里哪里发生了问题……
 
------最佳解决方案--------------------
既然有spring,为什么不extends ContextLoaderListener呢
------其他解决方案--------------------
<ref bean="sysConfigService" />
sysConfigService配置了么?是不是这里的sysConfigService就是空的?

------其他解决方案--------------------
在Action里注入了吗
------其他解决方案--------------------
该回复于2010-09-15 16:35:46被版主删除
------其他解决方案--------------------
引用:
既然有spring,为什么不extends ContextLoaderListener呢

这个另外考虑,对1楼,因为这个是服务启动时加载数据,所以没有涉及到action因而不需要在action中注入,对2楼,sysConfigService我做了配置.
spring注入,为什么对一个普通bean就不行呢?请高手指点
------其他解决方案--------------------
我做了如下修改,在web.xml中增加了
<listener> 
    <listener-class> 
        org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener>
为的是优先加载applicationContext.xml,然后我在Configer类的this.sysConfigService = sysConfigService;处设置了断点,经过跟踪是可以到达这里的,并且,有值,输出为
com.family168.service.impl.EpareSysConfigImpl@196a21e
可是继续向下跟踪,当执行initSystemListener 类中Configer.getInstance("systemConfig").loadSysConfig();方法时,因为该方法调用了sysConfigService,结果此时该值又
变为了null!!!!
头大啊,怎么回事啊?????已经付好的值哪里去了啊!!!!

------其他解决方案--------------------
无人理我哦
------其他解决方案--------------------
我也遇到了这个问题..服务器启动时用servlet启动一个线程..里面有需要注入的对象...但一直为null..不知道楼主解决了这个问题了没有啊?