hanumant
hanumant

Reputation: 1101

Spring 2.5 to 4.2 upgrade issue - BeanWrapperImpl

I am upgrading Spring 2.5 to 4.2. The issue is with one bean which has property type org.springframework.core.io.ClassPathResource. The resource value is defined in xml as p:location="classpath:/<the resource path>"

This worked perfect and bean property was populated with the resource. But in 4.2 the value is not getting set.

So I debugged the code and found that the class org.springframework.beans.BeanWrapperImpl was manipulating the value and removing classpath: string from actual value in Spring 2.5.

However the same is not true in 4.2 and class org.springframework.beans.BeanWrapperImpl isn't modifying the value which results in spring not finding the resource.

Anyone faced similar situation? What solution did you apply?

Thanks, Hanumant

EDIT 1: code sample

spring config file

    <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();
    }

}

}

Error Message

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)

However if I remove the classpath: from bean definition it works.

So is classpth: necessary in bean definition xml file?? And why it was working fine in Spring 2.5??

Upvotes: 1

Views: 218

Answers (1)

M. Deinum
M. Deinum

Reputation: 124622

The main issue is that you aren't programming to interfaces. Instead of the concrete org.springframework.core.io.ClassPathResource use org.springframework.core.io.Resource. When doing to the org.springframework.core.io.ResourceEditor will kick in and convert the String into a Resource instance. The location you are providing classpath:/<the resource path> will be passed to the ResourceLoader which will get the resource or throw an error if it doesn't exist.

If, however, you are using the concrete type ClassPathResouce directly this mechanism doesn't kick in and the location is set to what you provide classpath:/<the resource path>. However this is actually not a valid location for the URL class and that will eventually fail with the message you see.

It worked in earlier versions due to a hack/workaround/patch in the BeanWrapperImpl to strip the prefix.

Basically it now fails because you where doing things you shouldn't have been doing in the first place.

Upvotes: 1

Related Questions