被注入类的set方法都被调用了,可以system.out.println()东西出来了,但是在测试类里面etomSystemCache一直都是null,一直报java.lang.NullPointerException上代码:
1。 被注入的类
  public class SystemCache {    /**
     * Singleton instance
     */
    private static SystemCache singleton = null;    private static boolean isGenCacheIndividually = false;
    
    private Object etomSystemCache;
    
    public void setEtomSystemCache(Object etomSystemCache)
{
     if(etomSystemCache != null)
     {
     try 
     {
    this.etomSystemCache = etomSystemCache.getClass().getMethod("singleton", null).invoke(etomSystemCache, null);

catch (Exception e) 
{

e.printStackTrace();

     }
    
}  private void populate(SystemDetail systemDetail)
    {
        if (systemDetail != null)
        {
            this.systemDetail = systemDetail;
        }
        else
        {
            this.systemDetail = new SystemDetail();
        }
    }
/**
     * Creates object.
     */
    private SystemCache()
    {
        super();
    }    /**
     * Returns the Singleton instance.
     */
    public static SystemCache singleton()
    {     if(!isGenCacheIndividually && singleton == null)
        {
     singleton = new SystemCache();
        }
     return singleton;
    }    /**
     * Populates and then returns the Singleton instance.
     */
    public static SystemCache singleton(SystemDetail systemDetail)
    {
        if (systemDetail != null)
        {
            if (singleton == null)
            {
                singleton = new SystemCache();
            }            singleton.populate(systemDetail);
        }        return singleton;
    }    /**
     * SystemDetail
     */
    private SystemDetail systemDetail = null;
    /**
     * Returns a copy of the businessDate.
     */
    public Date getBusinessDate()
    {
        if(isGenCacheIndividually)
        {
         return new Date(systemDetail.getBusinessDate().getTime());
        }
        else
        {
         Method getBusiMethod = null;
         Date businessDate = null;
        
         try
         {
getBusiMethod = etomSystemCache.getClass().getMethod("getBusinessDate", null);

         catch (Exception e) {

e.printStackTrace();

        
         try
         {
         businessDate = (Date)getBusiMethod.invoke(etomSystemCache, null);
        
} catch (Exception e) {

e.printStackTrace();
}
         return businessDate;
         //return this.existingCache.getBusinessDate();
        }
    }}
2。 注入类  
public class EtomSytemCache {
private EtomSytemCache()
    {
       
    }

private static EtomSytemCache singleton = null;

public static EtomSytemCache singleton(Collection<SystemPropertyDetail> systemPropertyList)
{
if (singleton == null)
{
singleton = new EtomSytemCache();
} return singleton;
}

public static EtomSytemCache singleton(){

if(singleton == null){
singleton = new EtomSytemCache();
}
return singleton;
}

Date businessDate = new Date(System.currentTimeMillis()); public Date getBusinessDate() {
return businessDate;
} public void setBusinessDate(Date businessDate) {
this.businessDate = businessDate;
}
}3。测试类  public class CacheTest { /**
 * @param args
 */
public static void main(String[] args) {

//SystemCache.singleton();
//EtomSytemCache.singleton();
//SystemCache.singleton().setEtomSystemCache(EtomSytemCache.singleton());

System.out.println("!!!"+SystemCache.singleton().getBusinessDate());
}}4。配置文件
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    
    <bean id="etomSytemCache"  class="com.hsbcpb.fos.oms.core.cache.EtomSytemCache"></bean>
    <bean id ="systemCache"  class="com.hsbcpb.fos.oms.core.cache.SystemCache">
      <property name="etomSystemCache" ref="etomSytemCache"/>
    </bean>
    
</beans>
 

解决方案 »

  1.   

    既然你都说是用Spring IOC和DI,但是你没有创建bean啊,你直接掉类里面的方法淡然空指针啊,你必须getBean(),
    ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
    再用ctx.getBean(“SystemCache ”);
    才能初始化
      

  2.   

    我找到原因了,报空指针是因为我调用的那个对象不是Spring实例化出来的那个对象,所以得不到Spring 注入的对象。
      

  3.   

    SystemCache.singleton().getBusinessDate()SystemCache这玩意 木有实例化...