我将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中工作正常?
请将实际配置添加到您的班级,而不是片段。 –
@ M.Deinum,我修改了原始问题,代码为 – hanumant
对于初学者来说,它应该是一个'Resource'而不是特定的类型。如果它不能被加载,那是因为它不存在于那个位置(这是异常告诉你的)。所以我猜想其他结构也会发生变化(也许你不仅仅是升级spring,还有其他框架/工具)。 –