被注入类的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。 被注入的类
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>
解决方案 »
- 急!struts1.3 打印图片问题 100分求教!
- 求一个简单而又精美的网页文本编辑器
- 怎么得到值????????????
- 哪位高手能帮帮我?~~ 关于 另存为 Web页,全部(*.htm;*.html) 的问题
- 怎么消除jsp中get方法传递中文参数出现乱码的情况????
- 什么时候用web project
- 高手请进:用ant编译struts应用时的错误.如果问题太简单,请莫笑
- 请教:如何让Linux在启动时自动加载Tomcat?
- 《即时应用Java Servlets》这本书的另外一个问题
- 利用eclipse自动的创建servlet是不是web.xml会自动写入?
- getClassInstance Line: 209 - com/opensymphony/xwork2/spring/SpringObject 请大家帮我看看
- httpclient请求IP地址设置问题,实现请求IP在多个IP间轮训
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
再用ctx.getBean(“SystemCache ”);
才能初始化