自定义 BeanFactory 取不到spring的配置的bean

2013-05-27 17:32:33 ERROR com.asiainfo.crm.service.inter.watch.WdProductSynServiceWathcherImpl:59  同步出错!程序异常!null
java.lang.NullPointerException
     at com.asiainfo.frame.web.ObjectFactory.getObject(ObjectFactory.java:37)
     at com.asiainfo.crm.core.common.SQLCommand.getSingleValue(SQLCommand.java:42)
     at com.asiainfo.crm.service.inter.watch.WdProductSynServiceWathcherImpl.getUrl(WdProductSynServiceWathcherImpl.java:35)
     at com.asiainfo.crm.service.inter.watch.WdProductSynServiceWathcherImpl.update(WdProductSynServiceWathcherImpl.java:43)
     at com.asiainfo.crm.service.inter.watch.WdProductSynServiceImpl.notifyWather(WdProductSynServiceImpl.java:48)
     at com.asiainfo.crm.service.inter.watch.WdProductSynServiceImpl.synWdProduct(WdProductSynServiceImpl.java:32)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:203)
     at $Proxy67.synWdProduct(Unknown Source)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:275)
     at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:276)
     at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
     at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)



com.asiainfo.frame.web.ObjectFactory.getObject(ObjectFactory.java:37)  这个ObjectFactory.getObject 方法是取spring 容器里面的bean
这里是获取jdbctemplte 的,配置没问题,这个问题很奇怪,发现开发和测试环境都没问题,就现网就问题。
第二天过来时,其它的同步程序又正常了。
原来这个问题是由于ObjectFactory 还没加载完全,定时 程序已经开始执行了。

public class ObjectFactory {
    protected static WebApplicationContext wac;
    private static ObjectFactory me;

    private ObjectFactory(WebApplicationContext wac) {
        ObjectFactory. wac = wac;
    }

    public static ObjectFactory getInstance(ServletContext servletContext) {
        if (me == null) {
            me = newObjectFactory(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext));
        }
        return me ;
    }

    public static ObjectFactory getInstance() {
        return me ;
    }

    public static Object getObject(String objname) {
        return wac .getBean(objname);
    }
}


public class AiContextLoaderListener extends ContextLoaderListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        super .contextInitialized(event);
        ServletContext servletContext = event.getServletContext();
        if (servletContext != null) {
            ObjectFactory. getInstance(servletContext);
        }
    }
}

这样在web.xml加载 AiContextLoaderListener  过滤器时,也就实例了 ObjectFactory


你可能感兴趣的:(spring,企业应用,自定义 BeanFactory)