SpringApplicationContextHelper初始化context失败

错误描述:
在接口Service中声明CmdbInfoWebServiceFunction时初始化失败,

    private CmdbInfoWebServiceFunction synchronizationDataWebServiceFunction = (CmdbInfoWebServiceFunction) SpringApplicationContextHelper.getContext().getBean("SynchronizationDataWebServiceFunction");

调试时发现SpringApplicationContextHelper.getContext()的值为空;
SpringApplicationContextHelper类如下:

package com.eshore.cmdb.common.base.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplicationContextHelper
{
    static{
        SpringApplicationContextHelper.context = new ClassPathXmlApplicationContext("spring.xml");
    }

    private static ApplicationContext context;

    /**
     * @return  context
     */
    public static ApplicationContext getContext()
    {
        return context;
    }

    /**
     * @param context: 你要设置的context 
     */
    public static void setContext(ApplicationContext context)
    {
        SpringApplicationContextHelper.context = context;

    }

    public static Object getBean(String beanStr)
    {
        return context.getBean(beanStr);
    } 

}

在该类中设置context为静态变量后错误消失

    static{
        SpringApplicationContextHelper.context = new ClassPathXmlApplicationContext("spring.xml");
    }

猜你喜欢

转载自blog.csdn.net/Ling1604/article/details/72928935