2016-06-15 119 views
1

我将Spring 2.5升级到4.2。 问题出在一个属性类型为org.springframework.core.io.ClassPathResource的bean上。资源值在xml中定义为p:location="classpath:/<the resource path>"春季2.5到4.2升级问题 - BeanWrapperImpl

这工作完美,bean属性已填充资源。但在4.2中,这个价值没有被设定。

所以我调试了代码,发现类org.springframework.beans.BeanWrapperImpl正在操纵该值并从Spring 2.5中删除实际值的classpath:字符串。

但是4.2中的情况并非如此,并且org.springframework.beans.BeanWrapperImpl类没有修改导致spring未找到资源的值。

任何人都面临类似的情况?你应用了什么解决方案?

谢谢, Hanumant

EDIT 1:代码示例

弹簧配置文件

<bean class="com.test.sample.TestBean" id="testBean" 
p:schemaLocation="classpath:/com/test/sample/Excalibur_combined.xsd" /> 

TestBean.java

public class TestBean { 
private ClassPathResource    schemaLocation; 

public ClassPathResource getSchemaLocation() { 
    return schemaLocation; 
} 

public void setSchemaLocation(ClassPathResource schemaLocation) { 
    this.schemaLocation = schemaLocation; 
} 

}

App.java

public class App { 
public static void main(String[] args) { 

    ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:/com/test/sample/spring-config.xml"); 
    TestBean tb = (TestBean) ap.getBean("testBean"); 
    try { 
     URL url = tb.getSchemaLocation().getURL(); 
     System.out.println(url); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

}

错误消息

INFO: Loading XML bean definitions from class path resource 
[com/test/sample/spring-config.xml] java.io.FileNotFoundException: 
class path resource 
[classpath:/com/test/sample/Excalibur_combined.xsd] cannot be resolved 
to URL because it does not exist at 
org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)>  at com.test.sample.App.main(App.java:20) 

但是如果我删除bean定义它的工作classpath:

那么classpth:是否需要在bean定义的xml文件中?为什么它在Spring 2.5中工作正常?

+0

请将实际配置添加到您的班级,而不是片段。 –

+0

@ M.Deinum,我修改了原始问题,代码为 – hanumant

+0

对于初学者来说,它应该是一个'Resource'而不是特定的类型。如果它不能被加载,那是因为它不存在于那个位置(这是异常告诉你的)。所以我猜想其他结构也会发生变化(也许你不仅仅是升级spring,还有其他框架/工具)。 –

回答

1

主要问题是您没有编程接口。而不是具体的org.springframework.core.io.ClassPathResource使用org.springframework.core.io.Resource。当做到org.springframework.core.io.ResourceEditor将踢入并将String转换为Resource实例。您提供的位置classpath:/<the resource path>将传递给ResourceLoader,该位置将获取资源或发生错误(如果不存在)。

但是,如果您直接使用具体类型ClassPathResouce,则此机制不会启动并且位置设置为您提供的内容classpath:/<the resource path>。但是,实际上这不是URL类的有效位置,并且最终会因您看到的消息而失败。

由于BeanWrapperImpl中的黑客/解决方法/修补程序剥去了前缀,它在早期版本中起作用。

基本上,它现在失败了,因为你在哪里做事情你不应该做的第一个地方。