DWR在和spring集成时,在dwr.xml中将设置creator="spring",告诉dwr将使用dwr的org.directwebremoting.spring.SpringCreator来创建对象实例,但是SpringCreator.getType地处理是不适当的,让我们来看看它的源码[dwr-3.0.0.116]:
public Class<?> getType()
{
if (clazz == null)
{
try
{
clazz = getInstance().getClass();
}
catch (InstantiationException ex)
{
log.error("Failed to instansiate object to detect type.", ex);
return Object.class;
}
}
return clazz;
}
我们再来看看它的getInstance,最终由spring来创建实例.
public Object getInstance() throws InstantiationException
{
try
{
if (overrideFactory != null)
{
return overrideFactory.getBean(beanName);
}
if (factory == null)
{
factory = getBeanFactory();
}
if (factory == null)
{
log.error("DWR can't find a spring config. See following info logs for solutions");
log.info("- Option 1. In dwr.xml, <create creator='spring' ...> add
log.info("- Option 2. Use a spring org.springframework.web.context.ContextLoaderListener.");
log.info("- Option 3. Call SpringCreator.setOverrideBeanFactory() from your web-app");
throw new InstantiationException("DWR can't find a spring config. See the logs for solutions");
}
return factory.getBean(beanName);
}
catch (InstantiationException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new InstantiationException("Illegal Access to default constructor on " + clazz.getName() + " due to: " + ex);
}
}
getInstance将返回由spring来创建的实例,很明显SpringCreator.getType有点多此一举,它先创建了实例,再从实例的getClass获取对象的类型,而spring的beanFactory.getType同样有此功能,但它不需要先创建实例.
也许写这位代码的仁兄是不知道spring beanFactory.getType这个方法吧!
我把SpringCreator.getType改正后的代码 如下:
public Class<?> getType()
{
if (clazz == null)
{
try
{
if(overrideFactory != null){
clazz=overrideFactory.getType(beanName);
}else {
if(factory == null)
factory = getBeanFactory();
clazz=factory.getType(beanName);
}
}
catch (Exception ex)
{
log.error("Failed to detect type.", ex);
return Object.class;
}
}
return clazz;
}
如果出现
Error loading class for creator ...... 那么就修改SpringCreator吧!